Expand description
A virtual machine and its runtime values.
§Examples
§Embedding Scheme scripts in Rust with a custom virtual machine
First, prepare a Scheme script named src/hello.scm
.
(import (scheme base))
(write-string "Hello, world!\n")
Then, add a build script at build.rs
to build the Scheme source file
into bytecode.
use stak_build::{build_r7rs, BuildError};
fn main() -> Result<(), BuildError> {
build_r7rs()
}
Finally, you can include the Scheme script into a Rust program using
include_module
macro and run the script.
use core::error::Error;
use stak::{
device::StdioDevice,
file::VoidFileSystem,
include_module,
process_context::VoidProcessContext,
module::Module,
r7rs::{SmallError, SmallPrimitiveSet},
time::VoidClock,
vm::Vm,
};
const HEAP_SIZE: usize = 1 << 16;
fn main() -> Result<(), Box<dyn Error>> {
// Include and run a Scheme script in the bytecode format built by the build script above.
run(&include_module!("hello.scm").bytecode())?;
Ok(())
}
fn run(bytecode: &[u8]) -> Result<(), SmallError> {
// Prepare a heap memory of a virtual machine.
let mut heap = [Default::default(); HEAP_SIZE];
// Create a virtual machine with its heap memory primitive procedures.
let mut vm = Vm::new(
&mut heap,
SmallPrimitiveSet::new(
// Attach standard input, output, and error of this process to a virtual machine.
StdioDevice::new(),
// Use void system interfaces for security because we don't need them for this example.
VoidFileSystem::new(),
VoidProcessContext::new(),
VoidClock::new(),
),
)?;
// Initialize a virtual machine with bytecode.
vm.initialize(bytecode.iter().copied())?;
// Run bytecode on a virtual machine.
vm.run()
}
Structs§
Enums§
Traits§
- Exception
- An exception.
- Primitive
Set - A primitive set.
- Profiler
- A profiler.
Type Aliases§
- Tag
- A tag.