lmcode-java-sample guide

This guide walks through the 10-step architecture of lmcode-java-sample, covering Java 21 (JVM & GraalVM Native Image) arena ownership, schema embedding, R49 rule dispatch, dry mode, and corpus verification.

Overview

src/main/java/io/lmctl/sample/Shop.java defines Money, Items, ShopState, and Ledger and binds all 45 domain signatures on io.lmctl.lmcode.Registry alongside the built-in lmcode_std_v3 catalog. src/main/java/io/lmctl/sample/Main.java loads the embedded sample.dsl.json classpath resource, handles discovery flags and multi-part builder invocations, invokes @item and @total R49 entries after each body, checks the non-negative ledger balance application rule, and writes the LMCODE_RETURN frame. Setting RUN_NATIVE=1 ./test.sh also builds and verifies a standalone GraalVM native binary.

Domain schema and registry bindings

The host registers two contexts (shop: Shop and ledger: Ledger), four object types (Money, Items, Shop, Ledger), the Currency enum, overloaded factory functions money(int) and money(float), arithmetic operator methods (plus, minus, times, neg), collection iteration methods (Items.len and Items.at), and two R49 SAM interfaces (Adjuster and Score). All 45 host signatures coexist with the default lmcode_std_v3 catalog.

Rule entry dispatch and standard interfaces

After the program body finishes, the host iterates Program.entries() in source order: every entry annotated @item is invoked once per shop item handle, and every entry annotated @total is invoked once with shop.total(). Non-void entry returns print 'entry NAME = VALUE' and increment entries_called.

./bin/lmcode-java-sample --version
./bin/lmcode-java-sample --lmcode-schema --json
./bin/lmcode-java-sample --lmcode-signatures
./bin/lmcode-java-sample --lmcode sample-suite/bytecode/001-literals.dslb

Invocation result protocol and dry mode

The sample CLI reads LMCODE_INVOCATION_FILE when present, propagates dry mode to Context before domain setup, allocates an independent ResultSink for each --lmcode, --first, or --last slot, and emits one terminal LMCODE_RETURN_BEGIN / LMCODE_RETURN_END frame after all bodies, @item and @total rule entries, and the non-negative ledger balance application check finish.

Conformance verification

Running ./test.sh verifies sdk.lock and suite.lock, compiles the host, audits that every standard and host signature is covered by the corpus, checks --version, --lmcode-schema [--json], and --lmcode-signatures, runs all 123 canonical bytecode cases byte-for-byte against expected stdout and exit codes (0, 2, 3, 4), verifies dry-mode sentinel isolation, and runs negative harness mutation controls.