runtime: add dharma-required functions to el_runtime.c and runtime/*.el

Add the following functions that dharma registry calls but were missing
from the El runtime:

el_runtime.c (consumed by the old build system via released SDK):
  - list_len, list_get — aliases for el_list_len/el_list_get (handlers.el)
  - json_array_push — append pre-encoded element to JSON array string
  - now_millis, unix_timestamp_ms, time_now_ms — ms-since-epoch aliases
  - log_info, log_warn — structured stderr log helpers
  - config — reads config from environment (alias for getenv)
  - http_patch — HTTP PATCH with Content-Type: application/json
  - http_post_engram — HTTP POST with optional X-API-Key header
  - http_get_engram — HTTP GET with optional X-API-Key header
  - str_to_bytes — encode string as JSON byte array [72,101,...]
  - bytes_to_str — decode JSON byte array back to string
  - hash_sha256 — SHA-256 hex digest using built-in sha256 impl

runtime/*.el (consumed by the new build system):
  - http.el: http_patch, http_post_engram, http_get_engram
  - time.el: now_millis, unix_timestamp_ms, time_now_ms
  - env.el: config, log_info, log_warn, list_len, list_get
  - json.el: json_array_push, bytes_to_str
  - string.el: str_to_bytes, hash_sha256 (via __sha256_hex seed)

el_seed.h / el_seed.c:
  - __sha256_hex primitive with self-contained SHA-256 implementation
This commit is contained in:
Will Anderson
2026-05-04 19:07:08 -05:00
parent 245eb2898e
commit 53f2df500d
8 changed files with 580 additions and 0 deletions
+28
View File
@@ -63,6 +63,34 @@ fn state_keys() -> String {
return __state_keys()
}
// DHARMA runtime helpers
// config read a configuration value from the environment.
// Returns "" if the variable is not set. Alias for env().
fn config(key: String) -> String {
return __env_get(key)
}
// log_info write an [INFO] log line to stdout.
fn log_info(msg: String) {
__println("[INFO] " + msg)
}
// log_warn write a [WARN] log line to stdout.
fn log_warn(msg: String) {
__println("[WARN] " + msg)
}
// list_len return the number of elements in a list. Alias for el_list_len.
fn list_len(lst: [String]) -> Int {
return el_list_len(lst)
}
// list_get return the element at index i in a list. Alias for el_list_get.
fn list_get(lst: [String], i: Int) -> String {
return el_list_get(lst, i)
}
// UUID generation
// uuid_new generate a new random UUID v4.
+29
View File
@@ -170,6 +170,35 @@ fn http_response(status: Int, headers_json: String, body: String) -> String {
return __http_response(status, headers_json, body)
}
// HTTP client PATCH
// http_patch performs an HTTP PATCH request with Content-Type: application/json.
fn http_patch(url: String, body: String) -> String {
return __http_do("PATCH", url, body, "{\"Content-Type\":\"application/json\"}", el_http_timeout_ms())
}
// HTTP client Engram variants (optional API key)
//
// These are used by dharma's db.el to talk to Engram nodes.
// The key parameter is the X-API-Key header value; pass "" for no auth.
// http_post_engram performs an HTTP POST with Content-Type: application/json
// and an optional X-API-Key header. If key is "" no auth header is added.
fn http_post_engram(url: String, key: String, body: String) -> String {
if str_eq(key, "") {
return __http_do("POST", url, body, "{\"Content-Type\":\"application/json\"}", el_http_timeout_ms())
}
return __http_do("POST", url, body, "{\"Content-Type\":\"application/json\",\"X-API-Key\":\"" + key + "\"}", el_http_timeout_ms())
}
// http_get_engram performs an HTTP GET with an optional X-API-Key header.
fn http_get_engram(url: String, key: String) -> String {
if str_eq(key, "") {
return __http_do("GET", url, "", "{}", el_http_timeout_ms())
}
return __http_do("GET", url, "", "{\"X-API-Key\":\"" + key + "\"}", el_http_timeout_ms())
}
// SSE Server-Sent Events streaming
//
// Usage pattern for an SSE handler:
+40
View File
@@ -157,6 +157,24 @@ fn json_build_array(items: [String]) -> String {
return result + "]"
}
// json_array_push append a pre-encoded JSON element to a JSON array string.
// elem must be a valid JSON fragment (e.g. "\"foo\"" or "42").
// Returns a new JSON array string with elem appended.
// Example: json_array_push("[]", "\"alice\"") -> "[\"alice\"]"
fn json_array_push(arr: String, elem: String) -> String {
let n: Int = json_array_len(arr)
if n == 0 {
return "[" + elem + "]"
}
// arr ends with ']'; insert before it
let inner_end: Int = str_last_index_of(arr, "]")
if inner_end < 0 {
return "[" + elem + "]"
}
let prefix: String = str_slice(arr, 0, inner_end)
return prefix + "," + elem + "]"
}
// json_escape_string escape a raw string so it can be safely embedded as a
// JSON string value.
//
@@ -171,3 +189,25 @@ fn json_escape_string(s: String) -> String {
let s5: String = str_replace(s4, "\t", "\\t")
return s5
}
// ---------------------------------------------------------------------------
// DHARMA byte decoding
// ---------------------------------------------------------------------------
// bytes_to_str decode a JSON array of integer byte values back to a string.
// "[104,105]" -> "hi"
// Inverse of str_to_bytes (defined in string.el). Defined here because it
// depends on json_array_len and json_array_get_string which live in this file.
fn bytes_to_str(arr: String) -> String {
let n: Int = json_array_len(arr)
if n == 0 { return "" }
let out: String = __str_alloc(n)
let i: Int = 0
while i < n {
let elem: String = json_array_get_string(arr, i)
let b: Int = __str_to_int(elem)
out = __str_set_char(out, i, b)
i = i + 1
}
return out
}
+29
View File
@@ -876,3 +876,32 @@ fn str_join(parts: [String], sep: String) -> String {
}
return result
}
// DHARMA byte encoding (str_to_bytes)
//
// str_to_bytes encode a string as a JSON array of unsigned byte values.
// "hi" -> "[104,105]"
// Used by db.el to store content in Engram JSON nodes as a byte array.
// Note: bytes_to_str (the inverse) is defined in json.el because it depends
// on json_array_get_string which is defined there.
fn str_to_bytes(s: String) -> String {
let n: Int = __str_len(s)
if n == 0 { return "[]" }
let result: String = "["
let i: Int = 0
while i < n {
let b: Int = __str_char_at(s, i)
if i > 0 { result = __str_concat_raw(result, ",") }
result = __str_concat_raw(result, __int_to_str(b))
i = i + 1
}
return __str_concat_raw(result, "]")
}
// Cryptographic hashing
// hash_sha256 return the SHA-256 hex digest of a string.
// Delegates to the __sha256_hex seed primitive.
fn hash_sha256(s: String) -> String {
return __sha256_hex(s)
}
+23
View File
@@ -400,3 +400,26 @@ fn uuid_new() -> String {
fn uuid_v4() -> String {
return __uuid_v4()
}
// ---------------------------------------------------------------------------
// DHARMA-compatible aliases millisecond-precision timestamps.
//
// now_millis, unix_timestamp_ms, and time_now_ms all return the same value:
// milliseconds since the Unix epoch. They exist because different parts of
// the dharma codebase use different names for the same concept.
// ---------------------------------------------------------------------------
// now_millis milliseconds since Unix epoch. Alias for time_now().
fn now_millis() -> Int {
return __time_now_ns() / 1000000
}
// unix_timestamp_ms same as now_millis.
fn unix_timestamp_ms() -> Int {
return __time_now_ns() / 1000000
}
// time_now_ms same as now_millis.
fn time_now_ms() -> Int {
return __time_now_ns() / 1000000
}