# mlua-rs/mlua architecture

> Architecture diagram of the GitHub repository mlua-rs/mlua, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mlua-rs/mlua
- Repository: https://github.com/mlua-rs/mlua

## Overview

##### Main Components

a) Core Components:
- Lua Runtime Integration Layer
- Rust API Layer
- Type Conversion System
- Memory Management System
- Error Handling System
- Serialization System (optional feature)
- Async Support System (optional feature)

b) Supporting Components:
- Build System
- Module System
- Testing Framework
- Benchmarking System

**┌─────────────────────────────────────────────────────────────┐**
- Application Layer                        │
- ┤
- mlua API Layer                        │
- ┬──────────────┬──────────────────────────────┤
- Rust Types  │ Lua Types    │     Feature Modules          │
- Conversion  │ Management   │ ┌────────┐ ┌──────────────┐  │
- System      │ System       │ │ Async  │ │Serialization │  │
- Support │ │  (serde)     │  │
- ┴──────────────┤ └────────┘ └──────────────┘  │
- Safety & Memory Management Layer                        │
- ┤
- Lua Runtime Integration                     │
- ┬──────────────┬───────────────┬───────────────┤
- Lua 5.4   │   Lua 5.3    │   Lua 5.2     │   Lua 5.1     │
- Support   │   Support    │   Support     │   Support     │
- ┼──────────────┼───────────────┼───────────────┤
- LuaJIT    │    Luau      │    Module     │   Vendored    │
- Support   │   Support    │    Support    │    Mode       │
- ┴──────────────┴───────────────┴───────────────┘

##### Implementation Details

- Type conversion paths between Rust and Lua
- Memory management flow
- Error handling and propagation
- Async runtime integration points
- Serialization pathways
- UserData implementation paths

##### Notes

- Version compatibility matrix
- Feature flag dependencies
- Safety boundaries
- Public API entry points

- Multiple Lua version support
- Safety-first design
- Flexible feature system
- Async capabilities
- Serialization support
- Module system support

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Application Layer"
        AppCode["Application Code"]:::app
    end

    subgraph "mlua API Layer"
        API["Core API Entry Point"]:::api
        click API "https://github.com/mlua-rs/mlua/blob/main/src/lib.rs"
        
        subgraph "Core Components"
            TypeSystem["Type System"]:::core
            click TypeSystem "https://github.com/mlua-rs/mlua/tree/main/src/types/"
            StateManagement["State Management"]:::core
            click StateManagement "https://github.com/mlua-rs/mlua/tree/main/src/state/"
            FunctionHandling["Function Handling"]:::core
            click FunctionHandling "https://github.com/mlua-rs/mlua/blob/main/src/function.rs"
            TableHandling["Table Handling"]:::core
            click TableHandling "https://github.com/mlua-rs/mlua/blob/main/src/table.rs"
            StringHandling["String Handling"]:::core
            click StringHandling "https://github.com/mlua-rs/mlua/blob/main/src/string.rs"
            ThreadSupport["Thread/Coroutine Support"]:::core
            click ThreadSupport "https://github.com/mlua-rs/mlua/blob/main/src/thread.rs"
            UserData["UserData System"]:::core
            click UserData "https://github.com/mlua-rs/mlua/tree/main/src/userdata/"
        end

        subgraph "Feature Modules"
            AsyncSupport["Async Support"]:::feature
            Serialization["Serialization System"]:::feature
            click Serialization "https://github.com/mlua-rs/mlua/tree/main/src/serde/"
        end
    end

    subgraph "Core Systems Layer"
        TypeConversion["Type Conversion System"]:::system
        click TypeConversion "https://github.com/mlua-rs/mlua/blob/main/src/conversion.rs"
        MemoryManagement["Memory Management System"]:::system
        click MemoryManagement "https://github.com/mlua-rs/mlua/blob/main/src/memory.rs"
        ErrorHandling["Error Handling System"]:::system
        click ErrorHandling "https://github.com/mlua-rs/mlua/blob/main/src/error.rs"
    end

    subgraph "Lua Runtime Integration"
        ModuleSystem["Module System"]:::runtime
        click ModuleSystem "https://github.com/mlua-rs/mlua/blob/main/src/luau/package.rs"

        subgraph "Lua Version Support"
            Lua54["Lua 5.4 Support"]:::version
            click Lua54 "https://github.com/mlua-rs/mlua/tree/main/mlua-sys/src/lua54/"
            Lua53["Lua 5.3 Support"]:::version
            click Lua53 "https://github.com/mlua-rs/mlua/tree/main/mlua-sys/src/lua53/"
            Lua52["Lua 5.2 Support"]:::version
            click Lua52 "https://github.com/mlua-rs/mlua/tree/main/mlua-sys/src/lua52/"
            Lua51["Lua 5.1 Support"]:::version
            click Lua51 "https://github.com/mlua-rs/mlua/tree/main/mlua-sys/src/lua51/"
        end

        subgraph "Additional Runtimes"
            LuaJIT["LuaJIT Support"]:::runtime
            Luau["Luau Support"]:::runtime
            click Luau "https://github.com/mlua-rs/mlua/tree/main/mlua-sys/src/luau/"
            VendoredMode["Vendored Mode"]:::runtime
        end
    end

    subgraph "Support Systems"
        BuildSystem["Build System"]:::support
        click BuildSystem "https://github.com/mlua-rs/mlua/tree/main/mlua-sys/build/"
        BenchmarkSystem["Benchmarking System"]:::support
        click BenchmarkSystem "https://github.com/mlua-rs/mlua/tree/main/benches/"
    end

    %% Relationships
    AppCode --> API
    API --> TypeSystem & StateManagement & FunctionHandling & TableHandling & StringHandling & ThreadSupport & UserData
    API --> AsyncSupport & Serialization
    TypeSystem & StateManagement & FunctionHandling & TableHandling & StringHandling & ThreadSupport --> TypeConversion
    TypeConversion & MemoryManagement & ErrorHandling --> ModuleSystem
    ModuleSystem --> Lua54 & Lua53 & Lua52 & Lua51
    ModuleSystem --> LuaJIT & Luau & VendoredMode
    BuildSystem --> ModuleSystem

    %% Styling
    classDef app fill:#f9f,stroke:#333,stroke-width:2px
    classDef api fill:#9cf,stroke:#333,stroke-width:2px
    classDef core fill:#9cf,stroke:#333,stroke-width:1px
    classDef feature fill:#ff9,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef system fill:#9f9,stroke:#333,stroke-width:2px
    classDef runtime fill:#c9f,stroke:#333,stroke-width:2px
    classDef version fill:#c9f,stroke:#333,stroke-width:1px
    classDef support fill:#ddd,stroke:#333,stroke-width:1px
```
