Motus

Integrations

From Jai today. Elsewhere, next.

Motus lives in this repository as sdk/Motus (the language and its VM) and sdk/Motus_Flows (entries, calls, actions, edge pages). The code below is from apps in this repository, read from their files when this site was built.

An entry

A part of an app — a dashboard, a site — is one template mounted at a path with the controllers whose procedures it calls. Argoshot mounts five. @Call procedures are its reads, @Action ones its forms'. The template compiles with the program: a misspelled call, an argument of the wrong type, a field the action does not take, is a build error.

examples/shots/app.jai:31
routes :: () {
    host := to_lower_copy(header("host"),, allocator = temp);
    if begins_with(host, "api.")  { Screenshots__linked.routes(); return; }   // (the API: the module's own routes)
    formats(accept = .FORM, respond = .HTML);
    if !is_same_origin_request()  { error(403, "cross-site form submission refused"); return; }
    motus_mount("/app", TEMPLATE_views_app, .[Account, Playground, Keys], TEMPLATE_views_layout);
    get("/", home);
} @Handler
examples/shots/dashboard.jai:31
// The account's keys: renamed.
Keys :: struct {
    save :: (p: struct { id: int; name: string; @MaxLength(60) }) -> (done: Motus_Done = .{}) {
        return .{notice = "Saved."};
    } @Action
}

A template rendered to a string

Emails, invoices, a fragment to push: motus_program(TEMPLATE_x, View) compiles with the program, so its errors are build errors, and motus_render(*program, *view) renders. The campaigns app sends its emails this way.

framework/Campaigns/library.jai:343
email_html :: (header_type: string, header: string, body_html: string, footer: string, buttons: [] Campaign_Button, pixel: string) -> string {
    program, failed := motus_program(TEMPLATE_email, Email_View);
    view := Email_View.{header_type, header, body_html, footer, buttons, pixel};
    html: string;
    if !failed  html, failed = motus_render(*program, *view);
    if failed  { flow_log_error("campaigns: an email's page: %", failed); return body_html; }
    return html;
}

A template someone else wrote

A merchant's store template is text, compiled when it is saved with untrusted = true: no scripts, no event handlers, no frames, forms post to the store, CSS checked. Its errors go back with their lines; the program is serialized and kept. This site's playground compiles visitors' templates the same way, and renders them within limits.

framework/Storefront/module.jai:106
compile_template :: (source: string) -> Motus_Program, errors: string {
    program, errors := motus_compile_with(source, type_info(Sf_View), options = .{untrusted = true});
    return program, errors;
}
play.jai:73
// The template, against the sample's type: its program, or its errors (one a line, "<line>:<column>: ...").
play_compile :: (source: string, view: *Type_Info) -> Motus_Program, errors: string {
    program, errors := motus_compile_with(source, view, options = .{untrusted = true, calls = play_calls(), types = .[type_info(Post_Params)]}, library = .[PLAY_KIT]);
    return program, errors;
}

An edge page

motus_mount(path, template, controllers, library, models = ..., params = ...): the models are what its queries read, params a struct of the query string's parameters the template reads by name, the controllers answer origin's part. The catalog example serves 20,000 parts this way; sdk/Motus/edge holds the worker.

examples/catalog/app.jai:219
routes :: () {
    get("/seed", seed);
    get("/signup", signup_page);
    post("/signup", signup);
    get("/login", login_page);
    post("/login", login);
    post("/logout", logout);
    get("/me", me);
    if begins_with(router().remaining, "/team/") && context.pass.request.method == "POST"  { router().remaining = slice(router().remaining, 5, router().remaining.count - 5); team(); return; }
    post("/restock", restock);
    post("/orders/refund", refund);
    get("/", home);
    motus_mount("/parts", TEMPLATE_views_parts, .[Catalog], TEMPLATE_views_shell, models = .[type_info(Part)], params = Parts_Query);
    motus_mount("/orders", TEMPLATE_views_orders, .[Catalog], TEMPLATE_views_shell, models = .[type_info(Order), type_info(Order_Line)], params = Orders_Query);
} @Handler
examples/catalog/app.jai:86
Catalog :: struct {
    Shop :: struct { name: string; total: int; categories: [] Category_Line; }
    Category_Line :: struct { key: string; name: string; parts: int; }   // (key: its name in addresses)

    // The frame's: made at origin, once (the edge keeps it, and drops it when a count changes). The
    // parts are the catalog's (public rows), whoever asks.
    shop :: () -> (shop: Shop = .{}) {
        lines := NewArray(CATEGORIES.count, Category_Line,, allocator = temp);
        total := 0;
        for public_rows()  for name, i: CATEGORIES {
            key := category_key(name);
            n := count(Part, #code it.category == key);
            lines[i] = .{key, name, n};
            total += n;
        }
        return .{"Acme Parts", total, lines};
    } @Call
}

Next

TypeScript, Rust, Rails

The plan is one standalone package around the same compiler and VM, built for wasm32, with two functions: compile(source, schema), which compiles a template against a description of its data and returns a program or its errors with lines and columns, and render(program, json), which renders a program with data given as JSON. Each host wraps those two.

Planned

A wasm32 core

The compiler and VM of Motus, freestanding, exporting compile and render. The VM already runs in WebAssembly (the edge worker's motus_edge.wasm); what it lacks is the compiler beside it and a schema in place of Jai's type information.

Planned

TypeScript: an npm package

The core with typings, for Node, Deno, Bun and workers: templates compiled at build time against a schema of your types, rendered on the server or at the edge.

Planned

Rust: a crate on wasmtime

The same .wasm, run by wasmtime behind a typed wrapper: a program compiled once and rendered per request, its errors surfaced at build time from a build.rs.

Planned

Rails: an ActionView handler

A template handler for .mot files that compiles each view against a schema of its locals and renders through the core, so a view that no longer fits its data fails in CI, not in production.

What has to exist first

Today motus_compile_with takes a Jai *Type_Info: the names a template reads, their types and their layout in memory all come from it, and the VM reads the view in place. A compile driven by a schema — JSON Schema in, the same checks out, the data laid out from JSON — is the piece the package needs, and it does not exist yet.