# openblockcc/openblock-vm architecture

> Architecture diagram of the GitHub repository openblockcc/openblock-vm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openblockcc/openblock-vm
- Repository: https://github.com/openblockcc/openblock-vm

## Overview

##### Project Type and Purpose
- The project is a virtual machine (VM) that powers a blocks-based programming environment (similar to Scratch) as indicated by the “openblock-vm” title.
- Its purpose is to interpret and execute block-based code, support extensions, integrate device interfaces (such as Arduino, micro:bit, etc.), handle input/output and communication, and serialize/deserialize project files.

##### File Structure – Key Top-Level Components
- Core Engine & VM:
- Files under “src/engine” (e.g., runtime.js, execute.js, thread.js) implement execution, sequencing, and runtime management.
- “src/virtual-machine.js” is the entry point that ties together the various engine components.
- Blocks Definition:
- “src/blocks” contains individual block implementations (control, motion, sensing, etc.).
- Extension Support:
- The “src/extensions” directory hosts additional modules (e.g., scratch3_music, scratch3_translate, etc.) that extend the VM functionality.
- “src/extension-support” provides helper modules for registering, managing, and integrating these extensions.
- Devices Integration:
- The “src/devices” folder contains subdirectories for various hardware adapters (Arduino, micro:bit, etc.), encapsulating device-specific code.
- Common device interfaces are provided under “src/devices/common.”
- Dispatch & Communication:
- “src/dispatch” contains modules (central, shared, worker dispatch) that enable event passing, scheduling, and data flow between different runtime components, likely supporting multi-threading or worker contexts.
- Input/Output (IO):
- “src/io” manages various I/O functionalities (keyboard, mouse, BLE, etc.) needed for interfacing with the user and external systems.
- Import & Serialization:
- “src/import” and “src/serialization” handle data ingestion (e.g., importing costumes or sounds) and converting project formats (e.g., SB2 to SB3), respectively.
- Utilities & Libraries:
- “src/util” holds helper functions and common utilities.
- “src/lib” includes external libraries (for instance, a firmata module for hardware communication).

##### Architectural Patterns and Design Principles
- Modular Architecture: The project separates concerns by grouping related functionality in specific directories (engine, blocks, extensions, devices, IO).
- Plugin/Extension Model: The use of an “extensions” directory along with support modules indicates a design that allows easy integration of new features.
- Event-Driven and Dispatch Mechanism: Modules under “src/dispatch” ensure that different parts of the system communicate asynchronously, which is typical for VMs handling user interactions and real-time execution.
- Separation of Concerns: Each component (engine, blocks, devices, IO) is clearly separated, facilitating maintainability and scalability.

- VM/Core Runtime:
- Central “Virtual Machine” (src/virtual-machine.js) that ties everything together.
- “Engine” layer (src/engine/*) managing execution, sequencing, threading, and block-processing.
- Blocks:
- “Blocks” layer (src/blocks/*) which defines the behavior of individual block types (control, motion, etc.).
- Extensions:
- “Extension Support” (src/extension-support/*) and “Extensions” (src/extensions/*) layers that plug additional features into the VM.
- Device Interfaces:
- “Devices” layer (src/devices/*) that provides hardware adapter modules and common peripheral interfaces.
- Dispatch System:
- “Dispatch” layer (src/dispatch/*) that handles communication between engine components, possibly bridging main and worker threads.
- I/O Subsystem:
- “IO” layer (src/io/*) manages input sources (keyboard, mouse, BLE, etc.) and potentially video and serial communications.
- Import & Serialization:
- “Import” and “Serialization” layers (src/import/* and src/serialization/*) that handle file conversion and data persistence.
- Utilities & External Libraries:
- The “Util” and “Lib” modules provide support functions and integrations (e.g., firmata for microcontrollers).

##### Data Flow and Interactions
- User input (collected via IO modules) is directed to the VM, which dispatches events through its engine.
- The Engine processes blocks (from “src/blocks”) and coordinates execution through threads (using dispatch components).
- Extensions and device modules interact with the engine, enhancing functionality or handling hardware-specific commands.
- Serialization modules ensure that the state can be saved (converted to project files) and loaded back.
- The dispatch system ensures asynchronous communication between worker threads, extensions, and the main VM loop.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "User Interaction"
        IO["Input/Output (Keyboard,Mouse,BLE,etc.)"]:::io
    end

    subgraph "Core System"
        VM["Virtual Machine / Core Runtime"]:::core
        Engine["Engine (Execution,Threading,Sequencing)"]:::core
        Blocks["Blocks (Control,Motion,Data,etc.)"]:::core
        Dispatch["Dispatch (Event-Driven Communication)"]:::dispatch
    end

    subgraph "Extensions & Devices"
        Extensions["Extensions (Plugin/Extension Model)"]:::plugin
        Devices["Devices (Hardware Adapters)"]:::device
    end

    subgraph "Data Management"
        Import["Import & Serialization (File Conversion & Data Persistence)"]:::importfill
    end

    Utilities["Utilities & Libraries"]:::util

    %% Data Flow and Relationships
    IO -->|"input"| VM
    VM -->|"executes"| Engine
    Engine -->|"processes"| Blocks
    Engine -->|"manages_events"| Dispatch
    Dispatch -->|"notifies"| Extensions
    Dispatch -->|"communicates"| Devices
    Engine -->|"saves_loads"| Import
    Utilities -->|"support"| VM

    %% Click Events
    click VM "https://github.com/openblockcc/openblock-vm/blob/main/src/virtual-machine.js"
    click Engine "https://github.com/openblockcc/openblock-vm/tree/main/src/engine/"
    click Blocks "https://github.com/openblockcc/openblock-vm/tree/main/src/blocks/"
    click Extensions "https://github.com/openblockcc/openblock-vm/tree/main/src/extensions/"
    click Devices "https://github.com/openblockcc/openblock-vm/tree/main/src/devices/"
    click Dispatch "https://github.com/openblockcc/openblock-vm/tree/main/src/dispatch/"
    click IO "https://github.com/openblockcc/openblock-vm/tree/main/src/io/"
    click Import "https://github.com/openblockcc/openblock-vm/tree/main/src/import/"
    click Utilities "https://github.com/openblockcc/openblock-vm/tree/main/src/util/"

    %% Styles
    classDef core fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef plugin fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef io fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef device fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
    classDef dispatch fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef importfill fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef util fill:#E8F8F5,stroke:#16A085,stroke-width:2px;
```
