add runtime/time.el, math.el, state.el — time, math, and state in El

Migrates the time, math/float, and in-process state surfaces from
el-compiler/runtime/legacy/el_runtime.c to self-hosted El source:

- runtime/time.el: time_now, sleep_secs/ms, time_to_parts (via pure-El
  Gregorian civil_from_days decomposition), time_format (ISO + strftime
  subset), time_add, time_diff, time_from_parts; full Instant/Duration
  nanosecond API (now, unix_seconds/millis, duration_seconds/millis,
  instant_to_iso8601, sleep_duration); TTL cache (ttl_cache_set/get/age
  backed by state); uuid_new / uuid_v4 via __uuid_v4 seed.

- runtime/math.el: el_abs, el_max, el_min (Int); math_sqrt/log/ln/sin/cos/pi
  (Float seed wrappers); float_to_str, int_to_float, float_to_int, str_to_float,
  format_float (__format_float seed), decimal_round (half-away-from-zero via
  pure-El _pow10/_floor_f helpers).

- runtime/state.el: state_set/get/del/keys thin wrappers over __state_* seeds;
  convenience helpers state_has and state_get_or.
This commit is contained in:
Will Anderson
2026-05-03 15:39:48 -05:00
parent 9d0e1f64d4
commit 5678745381
3 changed files with 633 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
// runtime/state.el In-process key/value store.
//
// Thin El wrappers over the __state_* seed primitives. The backing store is
// a process-wide hash map maintained by the El runtime (formerly el_runtime.c
// lines 46324721: state_set, state_get, state_del, state_keys).
//
// Keys and values are Strings. Values are persistent across request boundaries
// within the same process instance (they survive individual request lifetimes).
// Concurrent access is serialized by the runtime; these wrappers are lock-free
// from El's perspective.
//
// Seed primitives consumed:
// __state_set(key: String, val: String)
// __state_get(key: String) -> String
// __state_del(key: String)
// __state_keys() -> String (JSON array of key strings)
// ---------------------------------------------------------------------------
// Core set / get / del / keys
// ---------------------------------------------------------------------------
// state_set store val under key. Overwrites any existing value.
fn state_set(key: String, val: String) {
__state_set(key, val)
}
// state_get retrieve the value for key. Returns "" if key is absent.
fn state_get(key: String) -> String {
return __state_get(key)
}
// state_del remove key from the store. No-op if key does not exist.
fn state_del(key: String) {
__state_del(key)
}
// state_keys return a JSON array string of all current keys.
// e.g. ["foo","bar","baz"]
// Matches legacy state_keys() which returns an ElList (here serialized as JSON).
fn state_keys() -> String {
return __state_keys()
}
// ---------------------------------------------------------------------------
// Convenience helpers
// ---------------------------------------------------------------------------
// state_has true if key is present (value is non-empty string).
// Note: a key set to "" is indistinguishable from absent via state_get alone.
fn state_has(key: String) -> Bool {
let v: String = state_get(key)
if str_eq(v, "") { return false }
return true
}
// state_get_or return val for key, or default_val if key is absent.
fn state_get_or(key: String, default_val: String) -> String {
let v: String = state_get(key)
if str_eq(v, "") { return default_val }
return v
}