lmcode-java guide

This guide explains the io.lmctl.lmcode package design, byte-accurate VmString handling, R49 rule entry and REST/CLI dispatch, and GraalVM Native Image compilation.

Overview

lmcode-java relies exclusively on the Java 21 standard library with zero external Maven runtime dependencies and zero reflection, making it directly compatible with GraalVM Native Image. Program.load validates the DSLB v6 header, checks the 32-byte SHA-256 schema hash, binds all imports against Registry, and verifies loop pairing and stack depth across the body and every R49 entry.

Registry and virtual machine embedding

Default new Registry() installs all 226 canonical lmcode_std_v3 signatures via Stdlib.register(registry). Host objects are bound in Context with context.setObject(name, type, instance) and passed as Value.object(type, instance). Callbacks implement Registry.Callback receiving (Context context, Value self, Value[] arguments). Overloads are distinguished by their parameter type list, and optional dry-mode preview callbacks are attached with Registry.dry(previewCallback).

REST and stateless CLI controller lifecycle

The 226-signature standard catalog includes the 20 REST/CLI functions and methods and 10 standard interfaces (RestGet..RestOptions, RestHandler, CliCommand, CliOption). Calling Rest.registerRest(registry, new RestOptions()) uses compatibility registration to bind those 20 signatures to a dedicated RestController. Endpoints can be declared with path annotations (@"/users/:id" on RestGet, @"status" on CliCommand, @"--verbose" on CliOption) or rest_route(method, path, annotation). Route templates support both :param / :rest... and {param} / {rest...} placeholders. Calling rest_config configures prefix or max_body_bytes, rest_cli(argv) dispatches stateless CLI commands via Program.callIsolated(), and rest_serve() pumps HTTP/1.1 requests over stdin/stdout once the body returns.

import io.lmctl.lmcode.*;
import io.lmctl.lmcode.rest.*;
import java.nio.file.*;

public final class HostMain {
    public static void main(String[] args) throws Exception {
        Registry reg = new Registry();
        RestController rest = Rest.registerRest(reg, new RestOptions().maxBodyBytes(1 << 20));
        byte[] bc = Files.readAllBytes(Path.of(args[0]));
        byte[] schema = Files.readAllBytes(Path.of(args[1]));
        Program prog = Program.load(bc, schema, reg);
        rest.run(prog, new Context());
    }
}

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 mvn -q verify or lmscript test.lms to run PinVerifier (226 std signatures), SelfTest, RestChecks (28 unit checks + 39 in-memory and 39 child-process REST/CLI scenarios), 410 core vectors, 271 stdlib vectors, 9 return fixtures, 28 invocation goldens, and 29 malformed-metadata goldens.