# bang590/jspatch architecture

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

- Interactive diagram: https://gitdiagram.com/bang590/jspatch
- Repository: https://github.com/bang590/jspatch

## Overview

##### Core Engine and Loader
- Core Engine: At the heart of the project is the JSPatch core (files like JPEngine.h, JPEngine.m, and JSPatch.js) that provides the runtime bridging. This component is responsible for starting the JavaScript engine, evaluating scripts, and invoking Objective-C methods from JavaScript.
- Loader: The Loader module (in the Loader directory) acts as a helper for fetching, unpacking, and (optionally) decrypting or packaging the scripts. It includes libraries such as RSA for security and ZipArchive for handling compressed assets.

##### Extensions and Additional Bridging Components
- The “Extensions” folder contains various modules (e.g., JPBlock, JPCFunction, JPCFunctionBinder, JPPlaygroundTool) that extend the capabilities of the core engine. Each extension adds support for extra functionality, such as handling blocks, C functions, or integrating with UIKit/CoreGraphics. These modules could be invoked from both the engine and the sample apps.
- Device-specific or specialized extensions (like JPLibffi, JPDispatch, JPProtocol, etc.) expand how Objective-C types and calls are bridged, making the system highly modular and extensible.

##### Demo Applications and Integration Examples
- The “Demo” directory includes multiple subprojects such as DribbbleDemo, OSXDemo, SwiftDemo, iOSDemo, iOSPlayground (and its tool demo), and tvOSDemo. Each of these is an example iOS (or macOS/tvOS) project that demonstrates how to integrate JSPatch into an application. The demos include sample code that initiates the engine ([JPEngine startEngine]), loads JavaScript (via a bundled script or from a network), and illustrates the bridging in action.
- The inclusion of several demos under different platforms (Objective-C and Swift, plus different device targets) shows the versatility and integration strategy the library promotes.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Demo Applications Subgraph
    subgraph "Demo Applications"
        demoDribbble("DribbbleDemo"):::demo
        demoOSX("OSXDemo"):::demo
        demoSwift("SwiftDemo"):::demo
        demoiOS("iOSDemo"):::demo
        demoPlayground("iOSPlayground"):::demo
        demoTool("iOSPlaygroundToolDemo"):::demo
        demotvOS("tvOSDemo"):::demo
    end

    %% Core Engine Node
    engine("JSPatch Core Engine\n(startEngine, evaluateScript)"):::engine

    %% Loader Subsystem Node
    loader("Loader Subsystem\n(fetch, unpack, decrypt)"):::loader

    %% Extensions Modules Subgraph
    subgraph "Extensions Modules"
        extensionJPBlock("JPBlock"):::extension
        extensionJPCFunction("JPCFunction"):::extension
        extensionJPCFunctionBinder("JPCFunctionBinder"):::extension
        extensionJPPlaygroundTool("JPPlaygroundTool"):::extension
        extensionJPProtocol("JPProtocol"):::extension
    end

    %% External Dependencies Node
    external("External Dependencies\nCocoaPods(podspec) & CI(.travis.yml)"):::dependency

    %% Connections from Demo Applications to Core Engine
    demoDribbble -->|"calls_startEngine"| engine
    demoOSX -->|"calls_startEngine"| engine
    demoSwift -->|"calls_startEngine"| engine
    demoiOS -->|"calls_startEngine"| engine
    demoPlayground -->|"calls_startEngine"| engine
    demoTool -->|"calls_startEngine"| engine
    demotvOS -->|"calls_startEngine"| engine

    %% Connection from Core Engine to Loader Subsystem
    engine -->|"dynamic_patching"| loader

    %% Connections from Core Engine to Extensions Modules (chained)
    engine -->|"addExtensions"| extensionJPBlock
    extensionJPBlock -->|"extend"| extensionJPCFunction
    extensionJPCFunction -->|"extend"| extensionJPCFunctionBinder
    extensionJPCFunctionBinder -->|"extend"| extensionJPPlaygroundTool
    extensionJPPlaygroundTool -->|"extend"| extensionJPProtocol

    %% Connection from Core Engine to External Dependencies
    engine -->|"integrated_with"| external

    %% Click Events for JSPatch Core Engine
    click engine "https://github.com/bang590/jspatch/blob/master/JSPatch/JPEngine.h"
    click engine "https://github.com/bang590/jspatch/blob/master/JSPatch/JPEngine.m"
    click engine "https://github.com/bang590/jspatch/blob/master/JSPatch/JSPatch.js"

    %% Click Events for Loader Subsystem
    click loader "https://github.com/bang590/jspatch/blob/master/Loader/JPLoader.h"
    click loader "https://github.com/bang590/jspatch/blob/master/Loader/JPLoader.m"
    click loader "https://github.com/bang590/jspatch/tree/master/Loader/libs"
    click loader "https://github.com/bang590/jspatch/blob/master/Loader/tools/packer.php"

    %% Click Events for Extensions Modules
    click extensionJPBlock "https://github.com/bang590/jspatch/tree/master/Extensions/JPBlock"
    click extensionJPCFunction "https://github.com/bang590/jspatch/tree/master/Extensions/JPCFunction"
    click extensionJPCFunctionBinder "https://github.com/bang590/jspatch/tree/master/Extensions/JPCFunctionBinder"
    click extensionJPPlaygroundTool "https://github.com/bang590/jspatch/tree/master/Extensions/JPPlaygroundTool"
    click extensionJPProtocol "https://github.com/bang590/jspatch/tree/master/Extensions/JPProtocol"

    %% Click Events for Demo Applications
    click demoDribbble "https://github.com/bang590/jspatch/tree/master/Demo/DribbbleDemo"
    click demoOSX "https://github.com/bang590/jspatch/tree/master/Demo/OSXDemo"
    click demoSwift "https://github.com/bang590/jspatch/tree/master/Demo/SwiftDemo"
    click demoiOS "https://github.com/bang590/jspatch/tree/master/Demo/iOSDemo"
    click demoPlayground "https://github.com/bang590/jspatch/tree/master/Demo/iOSPlayground"
    click demoTool "https://github.com/bang590/jspatch/tree/master/Demo/iOSPlaygroundToolDemo"
    click demotvOS "https://github.com/bang590/jspatch/tree/master/Demo/tvOSDemo"

    %% Click Events for External Dependencies
    click external "https://github.com/bang590/jspatch/blob/master/JSPatch.podspec"
    click external "https://github.com/bang590/jspatch/blob/master/.travis.yml"

    %% Styles
    classDef engine fill:#cce5ff,stroke:#003399,stroke-width:2px;
    classDef loader fill:#d1f2eb,stroke:#138d75,stroke-width:2px;
    classDef extension fill:#fcf8e3,stroke:#8a6d3b,stroke-width:2px;
    classDef demo fill:#f9e79f,stroke:#b7950b,stroke-width:2px;
    classDef dependency fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
```
