mrubyedge/lib.rs
1//! mruby/edge is a pure-Rust reimplementation of the mruby VM that keeps its
2//! core execution engine `no_std`-friendly while striving for behavioral
3//! compatibility with upstream mruby. It primarily targets WebAssembly
4//! deployments, yet remains embeddable inside ordinary Rust binaries for host
5//! tooling or native experimentation.
6//!
7//! Key goals:
8//! - Written in idiomatic Rust with a `no_std` core so it can run in
9//! constrained environments.
10//! - Behavior compatible with the mruby VM so existing bytecode executes as-is.
11//! - First-class WebAssembly target support.
12//! - Ergonomic embedding in general Rust applications.
13//!
14//! Basic initialization follows the pattern shown in `examples/newvm.rs`:
15//!
16//! ```no_run
17//! use mrubyedge::yamrb::{op, vm, value::RSym};
18//! use mrubyedge::rite::insn::{Fetched, OpCode};
19//!
20//! fn main() -> Result<(), Box<dyn std::error::Error>> {
21//! let irep = vm::IREP {
22//! __id: 0,
23//! nlocals: 0,
24//! nregs: 7,
25//! rlen: 0,
26//! code: vec![
27//! op::Op { code: OpCode::LOADI_1, operand: Fetched::B(1), pos: 0, len: 2 },
28//! op::Op { code: OpCode::LOADI_2, operand: Fetched::B(2), pos: 2, len: 2 },
29//! op::Op { code: OpCode::MOVE, operand: Fetched::BB(4, 1), pos: 4, len: 3 },
30//! op::Op { code: OpCode::MOVE, operand: Fetched::BB(5, 2), pos: 7, len: 3 },
31//! op::Op { code: OpCode::ADD, operand: Fetched::B(4), pos: 10, len: 2 },
32//! op::Op { code: OpCode::SSEND, operand: Fetched::BBB(3, 0, 1), pos: 12, len: 4 },
33//! op::Op { code: OpCode::RETURN, operand: Fetched::B(3), pos: 16, len: 2 },
34//! op::Op { code: OpCode::STOP, operand: Fetched::Z, pos: 18, len: 1 },
35//! ],
36//! syms: vec![RSym::new("puts".to_string())],
37//! pool: Vec::new(),
38//! reps: Vec::new(),
39//! catch_target_pos: Vec::new(),
40//! };
41//!
42//! let mut vm = vm::VM::new_by_raw_irep(irep);
43//! let value = vm.run()?;
44//! println!("{:?}", value);
45//! Ok(())
46//! }
47//! ```
48//!
49//! Loading a precompiled `*.mrb` produced by `mrbc` is also straightforward
50//! using `include_bytes!`:
51//!
52//! ```no_run
53//! use mrubyedge::rite;
54//! use mrubyedge::yamrb::vm;
55//!
56//! // Bundle the compiled script at build time.
57//! const SCRIPT: &[u8] = include_bytes!("../examples/simple.mrb");
58//!
59//! fn run_embedded() -> Result<(), Box<dyn std::error::Error>> {
60//! let mut rite = rite::load(SCRIPT)?;
61//! let mut vm = vm::VM::open(&mut rite);
62//! let value = vm.run()?;
63//! println!("{:?}", value);
64//! Ok(())
65//! }
66//! ```
67pub mod error;
68pub mod eval;
69pub mod rite;
70pub mod yamrb;
71
72pub use error::Error;
73
74/// The version of the mrubyedge crate
75#[macro_export]
76macro_rules! version {
77 () => {
78 env!("CARGO_PKG_VERSION")
79 };
80}
81
82pub const VERSION: &str = version!();