# hsiaosiyuan0/slowjs architecture

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

- Interactive diagram: https://gitdiagram.com/hsiaosiyuan0/slowjs
- Repository: https://github.com/hsiaosiyuan0/slowjs

## Overview

##### Main components and modules
- Build System & Configuration: Represent the CMake build files, presets (CMakeLists.txt in the root, in qjs, qjsc, src, libs, etc.), and configuration directories (.clang-format, .clang-tidy, .editorconfig).
- Core Engine:
- Parser Subsystem: Show modules under src/parse (e.g., asm.c, block.c, expr.c, lex.c, parse.c, etc.) which handle parsing, lexical analysis, and AST construction.
- Virtual Machine (VM): Include the modules in src/vm (e.g., vm.c, exec.c, gc.c, dump.c, instr.h, and related intrinsic implementations in src/vm/intrins) that coordinate bytecode execution, garbage collection, and built-in routines.
- Runtime Utilities: Include initialization, conversion, and shared libraries like those in libs (e.g., libbf, libregexp, libunicode) plus utilities in src/utils.
- Debugger:
- Debugger Commands Flow: Illustrate the flow from receiving JSON formatted commands (launch, setBreakpoint, listStackframes, dumpStackframe, continue) and how these interact with the VM state.
- GC Dump Functionality: Represent the enhancements that dump GC managed objects. This component captures a snapshot (heap snapshot file) which is later analyzed using Chrome DevTools.
- Command Line Tools:
- qjs: The interactive REPL/engine executable that leverages the core engine and integrates the debugger when invoked with command-line options.
- qjsc: The command-line compiler component that compiles JavaScript code based on the same underlying engine.
- Testing and Examples:
- Testing Suite: Show the tests directory (unit tests, test262, microbench, etc.) which validate different aspects of the engine and its extended functionalities.
- Examples: The examples directory illustrates basic usage scenarios in multiple languages (C, JavaScript).
- Documentation & Assets:
- Documentation (docs, imgs): Contains visuals (drawio assets) that likely represent various aspects such as AST, VM operation, call conventions, etc.

##### Relationships and interactions
- From user input (scripts run via qjs) → Parser → AST → Bytecode generation → Virtual Machine.
- Hook in the Debugger: When started in debug mode, the running process listens on a network port, intercepting execution at breakpoints.
- GC Dump: The engine triggers a GC dump function at designated points, which then writes out a snapshot file.
- That qjsc interacts with the same underlying engine but compiles JavaScript to bytecode or binary representation.
- Build flow where CMake compiles separate source directories into final executables (qjs, qjsc, tests).

##### Architectural patterns and design principles
- Modularity: The separation into folders (parse, vm, utils) demonstrates a modular design to break down a complex system into manageable subcomponents.
- Separation of Concerns: Debugging and GC dump functionalities are extended modules that hook into distinct parts of the engine’s lifecycle.
- Layered architecture: The flow from high-level JavaScript code → parser → compiler to bytecode → VM execution represents a layered design.
- Integration: The debugger’s design to accept JSON commands via TCP (using a simple nc client for debugging) shows how external protocols can interact with a native interpreter.

##### Final checklist for the principal software engineer
- For example, show how the debugger intercepts VM execution and communicates back to the user.
- Maintain a high level of detail by breaking down components into their respective submodules (as indicated by the file tree), without oversimplifying the system’s architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Build System & Configuration
    subgraph "Build System & Configuration"
        BS1("CMakeLists.txt"):::build
        BS2("CMakePresets.json"):::build
        BS3(".clang-format"):::build
        BS4(".clang-tidy"):::build
        BS5(".editorconfig"):::build
        BS6("qjs CMakeLists.txt"):::build
        BS7("qjsc CMakeLists.txt"):::build
        BS8("src CMakeLists.txt"):::build
        BS9("libs CMakeLists.txt"):::build
    end

    %% Core Engine
    subgraph "Core Engine"
        subgraph "Parser Subsystem"
            P("Parser (asm.c, block.c, expr.c, lex.c, parse.c...)"):::core
        end
        subgraph "Virtual Machine"
            VM("Virtual Machine (vm.c, exec.c, gc.c, dump.c, instr.h, intrinsics)"):::core
        end
        subgraph "Runtime & Utilities"
            UT1("Libraries (libbf, libregexp, libunicode)"):::core
            UT2("Utilities (char.c, dbuf.c, kid.c)"):::core
        end
    end

    %% Debugger & GC
    subgraph "Debugger & GC"
        subgraph "Debugger Engine"
            D1("qjs (qjs.c)"):::debug
            D2("Debugger (debug.c)"):::debug
            D3("REPL (repl.js)"):::debug
        end
        G("GC Dump Module"):::gc
    end

    %% Command Line Tools
    subgraph "Command Line Tools"
        T1("qjs CLI"):::tool
        T2("qjsc CLI"):::tool
    end

    %% Testing & Documentation
    subgraph "Testing & Documentation"
        TE1("Tests"):::test
        TE2("Examples"):::test
        Doc1("Documentation"):::doc
        Doc2("Assets"):::doc
    end

    %% External User Input
    U("User Script"):::tool

    %% Data Flow and Dependencies
    U -->|"runs"| T1
    T1 -->|"parses"| P
    P -->|"AST_to_Bytecode"| VM
    VM -->|"executes"| UT1
    VM -->|"executes"| UT2
    VM -->|"triggers_GC_dump"| G
    T1 -->|"debug_mode"| D1
    D2 -->|"intercepts"| VM
    T2 -->|"compiles_to_bytecode"| P
    T2 -->|"compiles_with"| VM

    %% Build Dependencies
    BS1 -->|"build_dep"| P
    BS1 -->|"build_dep"| VM
    BS1 -->|"build_dep"| UT1
    BS1 -->|"build_dep"| UT2
    BS1 -->|"build_dep"| TE1

    %% Styles
    classDef build fill:#cfe2f3,stroke:#0b5394,stroke-width:2px;
    classDef core fill:#d9ead3,stroke:#6aa84f,stroke-width:2px;
    classDef debug fill:#fff2cc,stroke:#bf9000,stroke-width:2px;
    classDef gc fill:#f4cccc,stroke:#cc0000,stroke-width:2px;
    classDef tool fill:#ead1dc,stroke:#b8549b,stroke-width:2px;
    classDef test fill:#d0e0e3,stroke:#1155cc,stroke-width:2px;
    classDef doc fill:#fce5cd,stroke:#e69138,stroke-width:2px;

    %% Click Events for Build System & Configuration
    click BS1 "https://github.com/hsiaosiyuan0/slowjs/blob/main/CMakeLists.txt"
    click BS2 "https://github.com/hsiaosiyuan0/slowjs/blob/main/CMakePresets.json"
    click BS3 "https://github.com/hsiaosiyuan0/slowjs/blob/main/.clang-format"
    click BS4 "https://github.com/hsiaosiyuan0/slowjs/blob/main/.clang-tidy"
    click BS5 "https://github.com/hsiaosiyuan0/slowjs/blob/main/.editorconfig"
    click BS6 "https://github.com/hsiaosiyuan0/slowjs/blob/main/qjs/CMakeLists.txt"
    click BS7 "https://github.com/hsiaosiyuan0/slowjs/blob/main/qjsc/CMakeLists.txt"
    click BS8 "https://github.com/hsiaosiyuan0/slowjs/blob/main/src/CMakeLists.txt"
    click BS9 "https://github.com/hsiaosiyuan0/slowjs/blob/main/libs/CMakeLists.txt"

    %% Click Events for Parser Subsystem
    click P "https://github.com/hsiaosiyuan0/slowjs/tree/main/src/parse"

    %% Click Events for Virtual Machine
    click VM "https://github.com/hsiaosiyuan0/slowjs/tree/main/src/vm"

    %% Click Events for Runtime Libraries & Utilities
    click UT1 "https://github.com/hsiaosiyuan0/slowjs/tree/main/libs"
    click UT2 "https://github.com/hsiaosiyuan0/slowjs/tree/main/src/utils"

    %% Click Events for Debugger Engine
    click D1 "https://github.com/hsiaosiyuan0/slowjs/blob/main/qjs/qjs.c"
    click D2 "https://github.com/hsiaosiyuan0/slowjs/blob/main/qjs/debug.c"
    click D3 "https://github.com/hsiaosiyuan0/slowjs/blob/main/qjs/repl.js"

    %% Click Event for GC Dump Module
    click G "https://github.com/hsiaosiyuan0/slowjs/blob/main/src/vm/dump.c"

    %% Click Events for Command Line Tools
    click T1 "https://github.com/hsiaosiyuan0/slowjs/blob/main/qjs/qjs.c"
    click T2 "https://github.com/hsiaosiyuan0/slowjs/blob/main/qjsc/qjsc.c"

    %% Click Events for Testing & Examples
    click TE1 "https://github.com/hsiaosiyuan0/slowjs/tree/main/tests"
    click TE2 "https://github.com/hsiaosiyuan0/slowjs/tree/main/examples"

    %% Click Events for Documentation & Assets
    click Doc1 "https://github.com/hsiaosiyuan0/slowjs/tree/main/docs"
    click Doc2 "https://github.com/hsiaosiyuan0/slowjs/tree/main/imgs"
```
