mquickjs-rs

Crates.iomquickjs-rs
lib.rsmquickjs-rs
version0.2.0
created_at2025-12-24 20:49:08.650438+00
updated_at2025-12-24 23:02:40.895384+00
descriptionSafe, idiomatic Rust wrapper for the MicroQuickJS engine
homepage
repositoryhttps://github.com/fcoury/mquickjs-rs
max_upload_size
id2003827
size75,714
Felipe Coury (fcoury)

documentation

README

mquickjs-rs

Safe, idiomatic Rust wrapper for the MicroQuickJS engine.

Installation

cargo add mquickjs-rs

Or in Cargo.toml:

mquickjs-rs = "0.2.0"

Usage

use mquickjs_rs::{Context, Runtime, Value};

let runtime = Runtime::new().expect("runtime should initialize");
let ctx = runtime.context().expect("context should initialize");

let sum = ctx.eval_i32("1 + 2 + 3", "example").expect("eval should succeed");
assert_eq!(sum, 6);

ctx.register_fn("echo", |args: &[Value<'_>]| Ok(args[0]))
    .expect("register should succeed");

let echoed = ctx
    .eval_string("echo('hello')", "example")
    .expect("eval should succeed");
assert_eq!(echoed, "hello");

Conversions

use mquickjs_rs::{Context, FromValue, IntoValue, Runtime};

let runtime = Runtime::new().expect("runtime should initialize");
let ctx = runtime.context().expect("context should initialize");

let value = 42i32.into_value(&ctx).expect("convert into JS");
let result = i32::from_value(value).expect("convert back");
assert_eq!(result, 42);

Objects and arrays

use mquickjs_rs::{Array, Context, Object, Runtime};

let runtime = Runtime::new().expect("runtime should initialize");
let ctx = runtime.context().expect("context should initialize");

let obj_value = ctx.eval("({})", "example").expect("eval should succeed");
let obj = Object::from_value(&ctx, obj_value).expect("object should wrap");
obj.set("name", "mquickjs").expect("set should succeed");
let name: String = obj.get("name").expect("get should succeed");
assert_eq!(name, "mquickjs");

let array_value = ctx.eval("[]", "example").expect("eval should succeed");
let array = Array::from_value(&ctx, array_value).expect("array should wrap");
array.push(1i32).expect("push should succeed");
let first: i32 = array.get(0).expect("get should succeed");
assert_eq!(first, 1);

Persistent handles

use mquickjs_rs::{Context, Persistent, Runtime};

let runtime = Runtime::new().expect("runtime should initialize");
let ctx = runtime.context().expect("context should initialize");

let value = ctx.eval("'hello'", "example").expect("eval should succeed");
let persistent = Persistent::new(&ctx, value).expect("persist should succeed");
let roundtrip = persistent.to_value().to_string().expect("to_string");
assert_eq!(roundtrip, "hello");

Notes

  • MicroQuickJS runs in a stricter ES5-like mode. See ../../mquickjs/README.md for engine limitations.
  • The JS context requires a preallocated memory buffer (minimum 1024 bytes).

License

MIT

Commit count: 0

cargo fmt