Skip to content

πŸŒ• LunaVM – A lightweight virtual machine with a custom memory allocator and MoonlightScript integration, designed for executing compiled opcodes or creating an extensible virtualized platform for 2D and 3D applications! πŸš€βœ¨

License

Notifications You must be signed in to change notification settings

CoffeeeAtNight/LunaVM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸŒ™ LunaVM

Welcome to LunaVM, a unique virtual machine built from scratch in C, designed to execute MoonlightScript programs, manage memory efficiently, and act as a foundation for a lightweight operating system or a virtualized platform. πŸš€


πŸ–₯️ Features

1. MoonlightScript Integration

LunaVM serves as the runtime environment for MoonlightScript, a statically typed, lightweight programming language designed to create system-level applications for LunarVM. MoonlightScript compiles to LunarVM opcodes, which the VM executes, providing a seamless bridge between high-level scripting and low-level execution.

Key features of MoonlightScript:

  • Static typing with type inference.
  • Clean, intuitive syntax for systems programming.
  • Compiles to binary opcodes for LunarVM (.lvmb format).
  • MoonlightScript programs can manipulate memory, render graphics, and even build multi-app systems.

2. Custom Memory Management with Starlock

LunaVM uses a custom-built Starlock allocator for dynamic memory management. Unlike traditional malloc-based allocation, Starlock:

  • Allocates memory in arenas to prevent fragmentation.
  • Tracks ownership and lifetimes of memory blocks to minimize memory leaks.
  • Ensures optimal performance for LunarVM programs.

3. Lightweight Virtual Machine

LunaVM interprets opcodes generated by the MoonlightScript Compiler (MLSC) and executes them in a secure, isolated environment. The VM provides:

  • A fetch-decode-execute cycle to handle instructions.
  • A stack-based and register-based hybrid architecture for flexibility.
  • Built-in support for key opcodes like MOV, ADD, PRINT, HLT.

4. Graphics Rendering with Raylib (Planned Integration)

Future versions of LunaVM will support Raylib to enable rendering of 2D/3D graphics directly from MoonlightScript programs, making it possible to:

  • Create windowed or fullscreen applications with dynamic content.
  • Develop interactive 3D games or simulations.
  • Bind MoonlightScript functions to Raylib’s rendering pipeline.

πŸš€ System Architecture

MoonlightScript Workflow

The development pipeline for LunaVM applications looks like this:

  1. Write MoonlightScript Code
    Example example.mls:

    fun main() (Void) {
      var message: String = "Hello, Lunar World!";
      print(message);
    }
    
  2. Compile with MoonlightScript Compiler (MLSC)

    • Generates .lvmb (binary) and .lvs (source-debug) files:
      • .lvmb: Binary file of opcodes for LunarVM.
      • .lvs: Optional source file for debugging and introspection.
  3. Run on LunaVM

    • Load the .lvmb file into LunaVM’s memory and execute:
      lunarvm example.lvmb

🌟 Virtual Machine Highlights

  1. Opcodes LunaVM currently supports the following opcodes:

    • MOV: Move data into registers.
    • ADD: Perform arithmetic.
    • PRINT: Display strings or numbers.
    • HLT: Halt execution.

    Example LunarVM program in assembly-like pseudocode:

    0x01 0x01 0x10 // MOV R1, 0x10
    0x02 0x01 0x01 // ADD R1, R1
    0x03           // PRINT R1
    0xFF           // HLT
    
  2. Memory Management

    • Starlock provides efficient allocation with ownership models and arena-based allocation.
    • LunarVM’s memory can be extended dynamically via Starlock, enabling applications to manage memory flexibly.
  3. Planned Multitasking

    • LunaVM will eventually support multiple loaded programs in separate memory spaces, enabling multitasking.

🌌 The Vision

The long-term goal of LunaVM is to evolve into a general-purpose virtual operating system capable of running multitasking applications, integrating features like:

  • A basic task scheduler to manage multiple processes.
  • Networking capabilities for communication between applications.
  • A file management system for .lvmb binaries and data.

πŸ”­ Example Use Case

  1. MoonlightScript Application Development
    Write applications like games, utilities, or simulations using MoonlightScript.

  2. Virtualized Operating System
    Build a lightweight OS using MoonlightScript to run multiple .lvmb programs concurrently on LunarVM.

  3. Graphics and Game Development
    Integrate Raylib to build interactive games or dynamic simulations in MoonlightScript.


❌ Contribution Policy

This project is a personal learning and exploration journey, so contributions are not currently accepted. However, feel free to fork and explore the code!


❀️ Special Thanks

Thanks for checking out LunaVM and supporting this exciting journey into virtual machines, memory management, and custom programming languages. 🌟


Let me know if you’d like to tweak anything further!

image image

About

πŸŒ• LunaVM – A lightweight virtual machine with a custom memory allocator and MoonlightScript integration, designed for executing compiled opcodes or creating an extensible virtualized platform for 2D and 3D applications! πŸš€βœ¨

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published