lmcode-rust guide
This guide covers embedding the dslvm crate in Rust applications, managing Context arenas and ByteString values, configuring RestController for REST and CLI dispatch, and running conformance checks.
Overview
The dslvm workspace is implemented purely with the Rust standard library. Registry stores sorted canonical signatures and callback closures. Program::load parses the DSLB v6 sections, verifies the 32-byte schema digest, resolves imports to dense callback slots, and runs structural control-flow and stack-depth verification on the body and every R49 entry.
Registry and virtual machine embedding
Host domain objects live inside Context as arena-indexed Box<dyn Any> slots referenced by ObjRef handles. Call ctx.insert(type_name, value) to allocate a handle and ctx.get::<T>(obj_ref) or ctx.get_mut::<T>(obj_ref) inside callbacks. Because handles are integer indices rather than raw pointers, host bindings require zero unsafe blocks. Registry::new() automatically registers all 226 lmcode_std_v3 signatures, including strings, hex/Base64 byte codecs, math, files, lists, maps, stacks, queues, priority queues, strict JSON, text templates, 24 lmcode_return_* sinks, and the 20 REST/CLI functions and methods.
REST and stateless CLI controller lifecycle
Calling dslvm::rest::register_rest(&mut registry, RestOptions::default()) attaches a RestController controller to the 20 standard REST/CLI signatures. Programs can declare routes with path-annotated entries implementing RestGet, RestPost, RestPut, RestDelete, RestPatch, RestHead, RestOptions, RestHandler, CliCommand, or CliOption, or dynamically via rest_route(method, path, annotation). Path templates accept both :param / :rest... and {param} / {rest...} syntax. Calling rest_config(key, val) sets prefix or max_body_bytes before rest_serve(). Calling rest_cli(argv) executes a stateless CLI command vector across CliOption, CliCommand, and REST handlers using an isolated entry invocation that preserves the outer run's collection arena. When the body calls rest_serve(), RestController::run pumps HTTP/1.1 requests sequentially from stdin to stdout.
use dslvm::rest::{register_rest, RestOptions};
use dslvm::{Context, Program, Registry, Value};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut registry = Registry::new();
registry.function("ping", &[], "string", |_ctx, _args| Ok(Value::string("pong")))?;
let mut rest = register_rest(&mut registry, RestOptions::default())?;
let schema = include_bytes!("../std/lmcode-std.schema.json");
let bytecode = std::fs::read("app.dslb")?;
let program = Program::load(&bytecode, schema, ®istry)?;
let mut ctx = Context::new();
rest.run(&program, ®istry, &mut ctx)?;
Ok(())
}
Invocation result protocol and dry mode
Every host invocation reads the optional version-1 manifest from LMCODE_INVOCATION_FILE to determine whether dry mode is active and to allocate one ResultSink per submitted program slot. Inside an _lmcode_ chunk, assignments to $return, $return.field, or $return[key] invoke the 24 standard lmcode_return_* imports on the active slot. After all bodies, R49 rule entries, and application checks complete, WriteFrame emits a single terminal LMCODE_RETURN_BEGIN / LMCODE_RETURN_END envelope on stdout and returns exit code 0 on success, 2 on CLI usage error, 3 on load or runtime error, or 4 on application validation failure.
Conformance verification
Run cargo test and lmscript test.lms to execute unit tests, verifier mutation gates, 410 core vectors, 271 stdlib vectors, 39 REST/CLI conformance scenarios (both in-memory and via the rest-serve child binary), return-frame vectors, and the router integration test.