Rmquickjs - High-level MicroQuickJS bindings for Rust

โš“ Rust    ๐Ÿ“… 2026-04-25    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 1      

surdeus

I have released rmquickjs, a crate that provides high-level Rust bindings for MicroQuickJS.

This includes a high-level Rust API that wraps MicroQuickJS's C API, as well as the ability to call functions from both JS and Rust. This crate is also available in no_std.

use rmquickjs::*;

fn main() -> Result<()> {
    // Initialize the MicroQuickJS engine
    let ctx = Context::new();
    
    // Execute JS with eval()
    let result = ctx.eval("1 + 2")?;
    assert_eq!(result.to_i32(&ctx), Some(3));

    // Access global variables with globals()
    ctx.eval("var x = 'hello'")?;
    let x = ctx.globals().get("x")?;
    assert_eq!(x.to_string(&ctx), "hello".to_string());

    // Call JS functions from Rust
    ctx.eval(
        r#"
function add(x, y) {
    return x + y;
}"#,
    )?;

    let add = ctx.globals().get("add")?.to_function(&ctx).unwrap();

    let result = add.call(&[ctx.new_i32(1), ctx.new_i32(2)])?;
    assert_eq!(result.to_i32(&ctx), Some(3));

    // Call Rust functions from JS
    let sub = ctx.new_function(|ctx, this, args| {
        if args.len() != 2 {
            ctx.throw(ctx.new_string("invalid number of arguments"))?;
        }
        let a = args[0].to_i32(ctx).unwrap();
        let b = args[1].to_i32(ctx).unwrap();
        Ok(ctx.new_i32(a - b))
    })?;
    ctx.globals().set("sub", sub);

    let result = ctx.eval("sub(1, 2)");
    assert_eq!(result.to_i32(&ctx), Some(-1));

    Ok(())
}

Have fun!

repo: GitHub - nuskey8/rmquickjs: High-level MicroQuickJS bindings for Rust ยท GitHub
crates.io: crates.io: Rust Package Registry

1 post - 1 participant

Read full topic

๐Ÿท๏ธ Rust_feed