javars 0.1.14

Java as a fusevm frontend: lexer, parser, and compiler to fusevm::Chunk, run on the shared three-tier Cranelift JIT — no bespoke VM or JVM
Documentation
//! Java wiring for inline Rust FFI (`rust { ... }` blocks).
//!
//! The heavy lifting lives in fusevm: [`fusevm::RustSugar`] scans and rewrites
//! the block at the source level, and `fusevm::ffi` compiles / loads / marshals
//! it. This module only supplies the Java-flavored [`fusevm::RustSugar`] config
//! and the desugar entry [`crate::parse`] calls before lexing. The emitted
//! `__rust_compile(...)` call and every exported bareword are resolved in
//! [`crate::compiler`] (the `JFFI_COMPILE` / `JFFI_CALL` print-style builtins)
//! and executed by [`crate::host`].
//!
//! A `rust { ... }` block must appear as a statement inside `main`'s body —
//! javars parses a single-class `main` — where the desugar replaces it in place
//! with a `__rust_compile("<base64>", <line>);` statement (note the trailing
//! `;`: Java statements are semicolon-terminated, unlike Kotlin).

use fusevm::RustSugar;

/// Emit the Java statement a `rust { ... }` block desugars to: a call to the
/// `__rust_compile` builtin carrying the base64-encoded block body and its line,
/// terminated by a `;` so it parses as a Java expression statement. base64's
/// alphabet (`A-Za-z0-9+/=`) has no `"` or `\`, so it needs no escaping inside
/// the double-quoted Java string literal.
fn emit(b64: &str, line: usize) -> String {
    format!("__rust_compile(\"{b64}\", {line});")
}

/// Java desugar config. Java line comments are `//`, block comments `/* */`.
/// `newline_boundary` is `true` so a `rust { ... }` block that starts a
/// statement line is recognized; `{`/`}`/`;` are boundaries too, so a block
/// after an opening brace or a preceding statement's `;` also matches.
pub const SUGAR: RustSugar = RustSugar {
    keyword: "rust",
    line_comments: &["//"],
    block_comment: Some(("/*", "*/")),
    newline_boundary: true,
    emit,
};

/// Rewrite every `rust { ... }` block in Java source into a `__rust_compile(...)`
/// statement, before lexing. No-op when the source has no `rust` token.
pub fn desugar(src: &str) -> String {
    SUGAR.desugar(src)
}

#[cfg(test)]
mod tests {
    #[test]
    fn desugars_block_inside_main() {
        let src = "class M {\n  public static void main(String[] a) {\n    rust { pub extern \"C\" fn add(a: i64, b: i64) -> i64 { a + b } }\n    System.out.println(add(2, 3));\n  }\n}\n";
        let out = super::desugar(src);
        assert!(out.contains("__rust_compile("), "no builtin call: {out}");
        assert!(out.contains(");"), "no statement terminator: {out}");
        assert!(!out.contains("pub extern"), "Rust body leaked: {out}");
        assert!(
            out.contains("System.out.println(add(2, 3))"),
            "trailing code lost: {out}"
        );
    }

    #[test]
    fn leaves_ordinary_java_untouched() {
        let src = "class M {\n  public static void main(String[] a) {\n    System.out.println(41 + 1);\n  }\n}\n";
        assert_eq!(super::desugar(src), src);
    }
}