🧅 Onion Virtual Machine
The runtime virtual machine for the Onion programming language. This package provides the execution environment, type system, and runtime services for Onion programs.
📚 Overview
The onion-vm package implements a register-based virtual machine designed specifically for functional programming with asynchronous execution capabilities. It features a sophisticated type system, garbage collection, and a generator-based execution model that enables seamless async/await functionality.
🏗️ Architecture
Bytecode → VM Instructions → Lambda Scheduler → Type System → Runtime Execution
Core Components
- Type System (
types/) - Comprehensive type definitions and operations - Lambda Runtime (
lambda/) - Function execution and scheduling - Scheduler (
lambda/scheduler/) - Asynchronous task coordination - Garbage Collection - Automatic memory management with reference counting
✨ Features
- 🚀 Asynchronous Execution - Generator-based coroutines with seamless async/await
- 🔒 Memory Safety - Automatic garbage collection and safe reference handling
- 🎯 Dynamic Type System - Flexible types with runtime type checking
- 📦 Modular Design - Clean separation between types, execution, and scheduling
- 🌊 Lazy Evaluation - Built-in support for lazy collections and streams
- 🔧 Extensible Runtime - Plugin architecture for custom types and operations
🎮 Type System
Core Types
- OnionObject - Base type for all Onion values
- OnionTuple - Immutable sequences and arrays
- OnionPair - Key-value pairs and associations
- OnionNamed - Named values and identifiers
- OnionLambda - Functions and closures
- OnionLazySet - Lazy collections with streaming operations
Type Features
use OnionObject;
use OnionTuple;
use OnionLambda;
// Creating and manipulating types
let obj = new_integer;
let tuple = from_vec;
let lambda = new;
🔄 Execution Model
Lambda Scheduler
The VM uses a sophisticated scheduler that manages:
- Coroutines - Lightweight tasks with cooperative scheduling
- Generators - Pausable functions with yield points
- Async Operations - Non-blocking I/O and computation
- Resource Management - Automatic cleanup and lifecycle management
Execution Flow
use Scheduler;
use Runnable;
// Create and run a task
let mut scheduler = new;
let task = /* create runnable task */;
scheduler.spawn;
scheduler.run_until_complete;
🚀 Usage
Basic VM Operation
use OnionObject;
use Scheduler;
// Create VM instance
let mut scheduler = new;
// Load and execute bytecode
let bytecode = /* load compiled bytecode */;
let result = scheduler.execute?;
Custom Type Integration
use ;
// Implement custom type
Async Programming
use ;
📋 API Reference
Core Types
OnionObject- Universal value type with runtime type informationOnionTuple- Immutable sequence type for collectionsOnionLambda- Function type with closure supportOnionLazySet- Lazy collection with functional operations
Runtime Services
Scheduler- Task scheduling and async coordinationRunnable- Trait for executable tasks and coroutinesGC- Garbage collection and memory management
Instruction Set
VMInstructionPackage- Compiled bytecode containerIRPackage- Intermediate representation formatIRTranslator- Converts IR to executable instructions
🔧 Dependencies
rust-arc-gc- Garbage collection implementationserde- Serialization support for bytecode and typesbincode- Binary serialization formatrustc-hash- High-performance hashing
🏗️ Module Structure
onion-vm/
├── src/
│ ├── lib.rs # Public API exports
│ ├── types/ # Type system implementation
│ │ ├── mod.rs
│ │ ├── object.rs # Base object type
│ │ ├── tuple.rs # Sequence types
│ │ ├── pair.rs # Key-value pairs
│ │ ├── named.rs # Named values
│ │ ├── lazy_set.rs # Lazy collections
│ │ └── lambda/ # Function types
│ │ ├── mod.rs
│ │ ├── definition.rs
│ │ └── vm_instructions/
│ └── lambda/ # Execution runtime
│ ├── mod.rs
│ ├── runnable.rs # Task execution interface
│ └── scheduler/ # Async scheduler
│ └── scheduler.rs
└── Cargo.toml
🎯 Design Principles
- Safety First - Memory safety through garbage collection and type checking
- Performance - Efficient execution with minimal overhead
- Concurrency - Native async support without blocking
- Simplicity - Clean abstractions that are easy to understand and extend
🔄 Execution Lifecycle
- Loading - Bytecode is loaded and validated
- Initialization - VM state and scheduler are set up
- Execution - Instructions are executed by the scheduler
- Yielding - Tasks can yield control for async operations
- Completion - Results are collected and returned
- Cleanup - Garbage collection reclaims unused memory
🤝 Integration
This package provides the runtime foundation for:
- onion-frontend - Executes compiled bytecode
- Standard Library - Implements built-in functions and types
- Language Extensions - Supports custom types and operations
- Debugging Tools - Provides introspection and profiling
📄 License
This package is part of the Onion programming language project and is licensed under the MIT License.