lmteavm

Quick reference for building a browser application with TeaVM-compiled Java, annotation-dispatched LMCode entries, and typed host functions.

Run the interactive demo, or clone the lmctl-ai/lmteavm source repository. The project is MIT licensed; TeaVM uses Apache-2.0, and the licenses are compatible.

Prerequisites

From a directory containing lmteavm and lmcode-java checkouts, run:

(cd lmcode-java && mvn -DskipTests install)
npm install --prefix compiler @lmctl-ai/lmctl@0.2.482
export LMCODE_COMPILER_MODULE="$PWD/compiler/node_modules/@lmctl-ai/lmctl/dist/dsl.js"
mvn -f lmteavm/pom.xml clean package

The generated site is written to lmteavm/target/demo/. To publish into another static site, set demo.output to an absolute destination:

mvn -f lmteavm/pom.xml -Ddemo.output=/absolute/site/path clean package

Architecture

  1. Java attaches browser events, fetches JSON, normalizes JavaScript values, and owns browser lifecycle.
  2. Maven runs the LMCode compiler before Java compilation and embeds the resulting bytecode and exact schema bytes in a generated Java class.
  3. Program.load verifies the bytecode against the schema and a Registry containing host callbacks.
  4. An LMCode entry has an annotation and implements a typed interface. Java resolves the annotation and calls the entry with Program.call.
  5. The entry calls typed host functions. Java implementations update the DOM, draw on canvas, use WebGL, or start browser animation.
  6. TeaVM compiles the application and runtime to teavm.js. An external boot.js calls main(); no inline script is required.

Define the contract

Declare each host function with parameter and return types. Declare interfaces for entries. The following is a minimal schema:

{
  "dsl.schema": 2,
  "name": "browser_app",
  "functions": {
    "dom_set_text": [{"params": ["string", "string"], "returns": "void"}]
  },
  "interfaces": {
    "DemoHandler": {"params": [], "returns": "void"}
  }
}

Add an annotated implementation in the LMCode block. The annotation is the dispatch key, and implements connects the function to the schema interface:

program = _lmcode_ schema="todo-handler.schema.json";
@hello
fn hello() -> void implements DemoHandler {
  dom_set_text("status", "Hello from LMCode");
}
end _lmcode_

Register host functions

Use a Registry without the standard library when the browser application needs only host functions. Register the exact same canonical signature as the schema:

Registry registry = new Registry(false);
registry.function("dom_set_text", List.of("string", "string"), "void",
    (context, self, args) -> {
        document.getElementById(args[0].asString().toString())
            .setInnerText(args[1].asString().toString());
        return Value.none();
    });

Load the generated bytecode and the unchanged schema bytes, then resolve and call the entry:

Program program = Program.load(bytecode, schemaBytes, registry);
int entry = findEntry(program, "hello");
program.call(entry, new Context(), registry, List.of());

findEntry is application code that scans program.entries() and compares Entry.annotation(). For parameters, pass Value.ofString, Value.ofInt, or Value.ofBool in interface order. Read a returned string with result.asString().toString().

Add browser events

Attach events in Java rather than embedding JavaScript in HTML. Use HTMLElement.onClick for clicks, EventTarget.onEvent for change and pointer events, Window.requestAnimationFrame for animation, and XMLHttpRequest for static JSON. Cancel animation frames before replacing their canvas. Disable controls during asynchronous loads when changing modes could otherwise make a stale response update the wrong view.

Use the JSON draw DSL

The demo loader accepts an object with title and commands. Each command has type, x, y, and color. Numeric fields that are omitted become zero; omitted strings become empty values.

TypeAdditional fieldsEffect
rectw, hFill a rectangle at x, y with size w, h.
linew, hDraw a line from x, y to w, h.
circlewFill a circle centered at x, y with radius w.
texttextDraw centered text at x, y.
{
  "title": "Small drawing",
  "commands": [
    {"type": "rect", "x": 20, "y": 20, "w": 200, "h": 80, "color": "#263d75"},
    {"type": "text", "x": 120, "y": 60, "color": "#edf2ff", "text": "Hello"}
  ]
}

The shape-scene and bar-chart options intentionally share the same loader and LMCode entries. Add a data-driven variant by supplying another JSON document, not another renderer.

Safety rules

Verify an application

Compile the LMCode and Java sources, then exercise real browser events. Check fetched URLs, rendered content, canvas or WebGL pixels, console errors, and page errors. The repository test starts its own static server and covers all seven demo modes:

npm ci
npx playwright install --with-deps chromium
npm test

GitHub Actions runs the same build and browser integration test using public dependencies. For website changes, also run the tools contract checks and the live integrity checker after deployment.