lehit 0.2.0

Embeddable Lehit language frontend and Cranelift JIT runtime
Documentation
use std::sync::atomic::{AtomicI32, Ordering};

use lehit::{
    core_lib::PRIMITIVE_MODULE_FILE_PATH,
    runtime::{ExecutionMode, RuntimeBuilder},
    rust_lib::{NativeFunction, RustLib, RustTypeRef, Script},
    semantic::{AccessPath, CanonicalSymbolPath},
};

const MODULE: &str = "native://integration.lt";

fn i32_type() -> RustTypeRef {
    RustTypeRef::Named(AccessPath {
        file_path: PRIMITIVE_MODULE_FILE_PATH.into(),
        local_access: "i32".into(),
        generics: Vec::new(),
    })
}

fn add(left: i32, right: i32) -> i32 {
    left.wrapping_add(right)
}

static LAST_RECORDED_VALUE: AtomicI32 = AtomicI32::new(0);

fn record(value: i32) {
    LAST_RECORDED_VALUE.store(value, Ordering::SeqCst);
}

#[test]
fn public_native_function_registration_executes_through_the_rust_library_unit() {
    let mut script = Script {
        content: "pub acs add: fn(left: i32, right: i32) -> i32\npub acs record: fn(value: i32)"
            .into(),
        ..Script::default()
    };
    // SAFETY: this function item has process lifetime, exactly matches the typed
    // registration, and wrapping arithmetic cannot panic or unwind.
    let native = unsafe { NativeFunction::from_rust_fn2("integration_add", add) };
    script
        .native_function("add", native)
        .public()
        .exported()
        .argument("left", i32_type())
        .argument("right", i32_type())
        .returns(i32_type())
        .finish();
    // SAFETY: this function item has process lifetime, its argument ABI matches
    // the registration, and atomic storage cannot panic or unwind.
    let native = unsafe { NativeFunction::from_rust_fn1_void("integration_record", record) };
    script
        .native_function("record", native)
        .public()
        .exported()
        .argument("value", i32_type())
        .finish();

    let mut library = RustLib::default();
    library.scripts.insert(MODULE.into(), script);
    let runtime = RuntimeBuilder::new()
        .with_execution_mode(ExecutionMode::Direct)
        .with_rust_lib(library)
        .build()
        .expect("valid native library must compile");

    let unit = runtime.rust_lib_unit().expect("native library unit");
    let path = CanonicalSymbolPath {
        module: MODULE.into(),
        local: "add".into(),
    };
    let call = unit
        .function_ptr(&path)
        .expect("registered native function");
    // SAFETY: registration above establishes this exact Rust ABI and the
    // function item remains valid for the process lifetime.
    let call: fn(i32, i32) -> i32 = unsafe { std::mem::transmute(call) };
    assert_eq!(call(20, 22), 42);

    let path = CanonicalSymbolPath {
        module: MODULE.into(),
        local: "record".into(),
    };
    let call = unit
        .function_ptr(&path)
        .expect("registered native function");
    // SAFETY: registration above establishes this exact Rust ABI and the
    // function item remains valid for the process lifetime.
    let call: fn(i32) = unsafe { std::mem::transmute(call) };
    LAST_RECORDED_VALUE.store(0, Ordering::SeqCst);
    call(42);
    assert_eq!(LAST_RECORDED_VALUE.load(Ordering::SeqCst), 42);
}