monkey-gc
QuickJS-style GC runtime for the Monkey programming language.
This crate is a GC-backed runtime that runs the same bytecode produced by
monkey-compiler, but stores Monkey runtime values in a GcHeap instead of
using Rc<Object> directly. It combines reference counting with a three-phase
cycle collector so cyclic object graphs can be reclaimed.
For the full design notes, see docs/gc.md.
Usage
Parse, compile, and execute Monkey source:
let result = eval_source.unwrap;
assert_eq!;
Run an already parsed program:
let program = parse.unwrap;
let result = eval.unwrap;
assert_eq!;
Use the lower-level VM when you need direct access to the heap:
let program = parse.unwrap;
let bytecode = compile.unwrap;
let mut vm = new;
vm.run;
vm.heap_mut.run_gc;
let result = vm.export_last_result.expect;
assert_eq!;
Public API
eval_sourceparses, compiles, and runs Monkey source.evalruns a parsed AST node through the bytecode compiler and GC VM.compilereusesmonkey-compilerto produce bytecode.GcVMexecutes Monkey bytecode with GC-managed values.GcHeapexposes allocation,dup/free, GC triggering, and heap inspection.Value,GcClosure,import_object, andexport_objectbridge between GC-managed values andobject::Object.
Runtime Model
monkey-gc is designed as a parallel runtime for the existing pipeline:
lexer -> parser -> compiler -> gc::GcVM
The crate does not replace the default compiler::VM, wasm, or playground
runtime. Instead, it reuses the same Bytecode and opcode definitions while
testing an alternate heap implementation.
Modules
heap.rsprovides the high-levelGcHeapand opaqueGcRefhandle.runtime.rsimplements reference counting and three-phase cycle collection.header.rs,list.rs, andmalloc.rshold GC object metadata and allocator accounting.value.rsdefines GC-managed Monkey values plus import/export helpers.frame.rsandvm.rsimplement the bytecode VM runtime.
Development
Run the crate tests from the workspace root:
Run the whole Rust workspace when changing shared compiler, object, or parser behavior: