# lynx-family/primjs architecture

> Architecture diagram of the GitHub repository lynx-family/primjs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lynx-family/primjs
- Repository: https://github.com/lynx-family/primjs

## Overview

PrimJS is a cross-platform JavaScript engine repo centered on a QuickJS-derived runtime, with PrimJS-specific performance, GC, debugging, and WebAssembly layers added on top for the Lynx ecosystem.

1. Core engine layer  
The heart of the repo lives under `src/interpreter/quickjs` and `src/interpreter/primjs`. The QuickJS subtree contains the language runtime, parser/compiler support, libc helpers, and versioning. The PrimJS subtree adds engine-specific integration points such as snapshot/embedded runtime assets for Android, iOS, and macOS, plus the build glue that produces the engine binary.

2. Memory management and GC boundary  
Unlike stock QuickJS reference counting, PrimJS has a separate GC subsystem under `src/gc`. This includes allocator/page management, marking/tracing, global and persistent handles, and GC logging/tracing. Architecturally, this is a major boundary: the engine core allocates and executes objects, while the GC layer owns lifecycle, heap layout, and tracing behavior.

3. Debugging and profiling subsystem  
`src/inspector` implements the developer-facing instrumentation layer. It is split into debugger, CPU profiler, heap profiler, and runtime/protocol support. This layer exposes runtime state and execution events to a Chrome DevTools Protocol-style consumer, so it sits alongside the engine rather than inside the language core.

4. Native module / N-API compatibility layer  
`src/napi` is a host-API abstraction for native addons. It contains common state, env/runtime management, and per-engine adapters for QuickJS, V8, JavaScriptCore, and Harmony. The `packages/weak-node-api` directory packages a related weak Node-API compatibility shim and CMake integration, showing that addon support is a first-class extension boundary rather than a monolithic part of the engine.

5. WebAssembly subsystem  
`src/wasm` provides JS-to-WASM interop, with shared types/utilities plus multiple backend implementations. There are engine-specific bindings for QuickJS and JavaScriptCore, and runtime abstractions for `prism` and `wasm3`. This indicates an adapter architecture: the repo defines a common WASM-facing API, then maps it onto different JS engines and WASM runtimes.

6. Embedded/runtime packaging for platforms  
Platform-specific integration lives under `Android`, `harmony`, and the platform subfolders in `src/interpreter/primjs`. Android includes a dedicated `PrimjsWasm` module and native registration glue; Harmony has its own app/module packaging and build scripts; the podspec at the root supports Apple distribution. These are packaging and embedding layers around the same core engine.

7. Command-line and developer tools  
`tools/qjs-cli` builds a standalone command-line entry point for the engine, while `tools/wasm3` and `tools/ci` provide build helpers and automation. This suggests the repo supports both embeddable library use and direct executable use, with the CLI serving as a thin host over the runtime.

8. Build system architecture  
The repository is multi-build-system: GN is the primary build graph for engine and tests, CMake is used for some native components and platform integrations, Gradle for Android, hvigor for HarmonyOS, and CocoaPods for Apple packaging. The architecture diagram should show build orchestration as a separate layer feeding multiple platform-specific outputs from the same source tree.

9. Testing and conformance  
`testing/quickjs` and `testing/wasm` contain engine and WASM test suites, including Test262-related scaffolding, debugger tests, GC tests, heap profiler tests, and JS API behavior tests. `tools/ci` scripts drive build/run validation. The repo’s quality gates are therefore split between language conformance, engine behavior, and WASM interoperability.

10. External dependencies and vendored code  
`third_party/wasm3` is vendored WASM runtime code, and `include/` contains public headers for QuickJS-style APIs and engine exports. The repo also carries patches for Test262 behavior. These are integration boundaries: external runtime code is embedded, but the public API surface is controlled by the headers and packaging layers in this repo.

11. Public surface area  
The root `include/` directory defines the C/C++ API surface exposed to embedders. This is the stable boundary that platform wrappers, N-API adapters, WASM bindings, and the CLI all sit on top of.

12. Overall flow  
Typical data flow is: source JavaScript or WASM enters through the CLI, an embedded host app, or a native addon path; the QuickJS/PrimJS runtime executes it; allocations and liveness are managed by the GC subsystem; optional debugger/profiler hooks observe execution; and WASM or N-API paths bridge out to native or alternate runtime implementations.

Main entry points: [`src/interpreter/quickjs`](https://github.com/lynx-family/primjs/tree/develop/src/interpreter/quickjs) (QuickJS Core), [`src/interpreter/primjs`](https://github.com/lynx-family/primjs/tree/develop/src/interpreter/primjs) (PrimJS Runtime), [`BUILD.gn`](https://github.com/lynx-family/primjs/blob/develop/BUILD.gn) (GN Build), [`testing/quickjs`](https://github.com/lynx-family/primjs/tree/develop/testing/quickjs) (QuickJS Tests)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core Engine"]
  node_quickjs_core["QuickJS Core<br/>js runtime"]
  node_primjs_runtime["PrimJS Runtime<br/>engine glue"]
  node_gc_subsystem[("GC System<br/>heap management")]
end

subgraph group_ext["Extensions"]
  node_inspector["Inspector<br/>debug/profiler"]
  node_napi["N-API Layer<br/>native addons"]
  node_wasm["WASM Bridge<br/>interop layer"]
  node_cli["QJS CLI<br/>host tool"]
end

subgraph group_platform["Platform Packaging"]
  node_android_pkg["Android"]
  node_harmony_pkg["HarmonyOS"]
  node_apple_pkg["Apple Podspec<br/>distribution<br/>[PrimJS.podspec]"]
end

subgraph group_quality["Quality & Validation"]
  node_build_gn{{"GN Build<br/>build graph<br/>[BUILD.gn]"}}
  node_cmake{{"CMake Build<br/>build system<br/>[CMakeLists.txt]"}}
  node_testing_qjs["QuickJS Tests<br/>engine tests"]
  node_testing_wasm["WASM Tests<br/>interop tests"]
end

subgraph group_deps["Dependencies"]
  node_include_api["Public API<br/>headers"]
  node_third_party_wasm3["wasm3<br/>vendored runtime"]
  node_patches_test262["Test262 Patch<br/>compat patch"]
end

node_quickjs_core -->|"allocates via"| node_gc_subsystem
node_primjs_runtime -->|"extends"| node_quickjs_core
node_primjs_runtime -->|"exports"| node_include_api
node_gc_subsystem -->|"publishes handles"| node_include_api
node_inspector -->|"observes"| node_quickjs_core
node_napi -->|"adapts"| node_quickjs_core
node_napi -->|"exposes"| node_include_api
node_wasm -->|"binds"| node_quickjs_core
node_wasm -->|"wraps"| node_third_party_wasm3
node_cli -->|"hosts"| node_quickjs_core
node_android_pkg -->|"embeds"| node_primjs_runtime
node_harmony_pkg -->|"embeds"| node_primjs_runtime
node_apple_pkg -->|"packages"| node_primjs_runtime
node_build_gn -->|"builds"| node_quickjs_core
node_build_gn -->|"builds"| node_primjs_runtime
node_build_gn -->|"builds"| node_inspector
node_build_gn -->|"builds"| node_napi
node_build_gn -->|"builds"| node_wasm
node_cmake -->|"supports"| node_napi
node_cmake -->|"supports"| node_android_pkg
node_testing_qjs -->|"validates"| node_quickjs_core
node_testing_qjs -->|"validates"| node_inspector
node_testing_qjs -->|"validates"| node_gc_subsystem
node_testing_wasm -->|"validates"| node_wasm
node_testing_wasm -->|"exercises"| node_third_party_wasm3
node_patches_test262 -->|"modifies"| node_testing_qjs

click node_include_api "https://github.com/lynx-family/primjs/tree/develop/include"
click node_quickjs_core "https://github.com/lynx-family/primjs/tree/develop/src/interpreter/quickjs"
click node_primjs_runtime "https://github.com/lynx-family/primjs/tree/develop/src/interpreter/primjs"
click node_gc_subsystem "https://github.com/lynx-family/primjs/tree/develop/src/gc"
click node_inspector "https://github.com/lynx-family/primjs/tree/develop/src/inspector"
click node_napi "https://github.com/lynx-family/primjs/tree/develop/src/napi"
click node_wasm "https://github.com/lynx-family/primjs/tree/develop/src/wasm"
click node_cli "https://github.com/lynx-family/primjs/tree/develop/tools/qjs-cli"
click node_android_pkg "https://github.com/lynx-family/primjs/tree/develop/Android"
click node_harmony_pkg "https://github.com/lynx-family/primjs/tree/develop/harmony"
click node_apple_pkg "https://github.com/lynx-family/primjs/blob/develop/PrimJS.podspec"
click node_build_gn "https://github.com/lynx-family/primjs/blob/develop/BUILD.gn"
click node_cmake "https://github.com/lynx-family/primjs/blob/develop/CMakeLists.txt"
click node_testing_qjs "https://github.com/lynx-family/primjs/tree/develop/testing/quickjs"
click node_testing_wasm "https://github.com/lynx-family/primjs/tree/develop/testing/wasm"
click node_third_party_wasm3 "https://github.com/lynx-family/primjs/tree/develop/third_party/wasm3"
click node_patches_test262 "https://github.com/lynx-family/primjs/tree/develop/patches/test262"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_quickjs_core,node_primjs_runtime,node_gc_subsystem toneBlue
class node_inspector,node_napi,node_wasm,node_cli toneAmber
class node_android_pkg,node_harmony_pkg,node_apple_pkg toneMint
class node_build_gn,node_cmake,node_testing_qjs,node_testing_wasm toneRose
class node_include_api,node_third_party_wasm3,node_patches_test262 toneIndigo
```

## Components

### Core Engine

QuickJS-derived runtime plus PrimJS integration and GC boundary.

- [QuickJS Core](https://github.com/lynx-family/primjs/tree/develop/src/interpreter/quickjs): js runtime · Language runtime, parser/compiler support, libc helpers, and versioning. · `src/interpreter/quickjs`
- [PrimJS Runtime](https://github.com/lynx-family/primjs/tree/develop/src/interpreter/primjs): engine glue · PrimJS-specific runtime integration and snapshot/build support. · `src/interpreter/primjs`
- [GC System](https://github.com/lynx-family/primjs/tree/develop/src/gc): heap management · Allocator, page layout, marking/tracing, handles, and GC logging. · `src/gc`

### Extensions

Developer tooling and host integration layers that sit beside the engine.

- [Inspector](https://github.com/lynx-family/primjs/tree/develop/src/inspector): debug/profiler · Debugger, CPU profiler, heap profiler, and protocol/runtime support. · `src/inspector`
- [N-API Layer](https://github.com/lynx-family/primjs/tree/develop/src/napi): native addons · Host API abstraction with engine adapters and environment/runtime management. · `src/napi`
- [WASM Bridge](https://github.com/lynx-family/primjs/tree/develop/src/wasm): interop layer · Common WASM API with engine- and runtime-specific adapters. · `src/wasm`
- [QJS CLI](https://github.com/lynx-family/primjs/tree/develop/tools/qjs-cli): host tool · Standalone command-line entry point for the engine. · `tools/qjs-cli`

### Platform Packaging

Platform-specific embedding and distribution targets.

- [Android](https://github.com/lynx-family/primjs/tree/develop/Android): package · Android app/module packaging and native registration glue. · `Android`
- [HarmonyOS](https://github.com/lynx-family/primjs/tree/develop/harmony): package · Harmony app/module packaging and build scripts. · `harmony`
- [Apple Podspec](https://github.com/lynx-family/primjs/blob/develop/PrimJS.podspec): distribution · CocoaPods packaging for Apple distribution. · `PrimJS.podspec`

### Quality & Validation

Tests, conformance, and CI automation.

- [GN Build](https://github.com/lynx-family/primjs/blob/develop/BUILD.gn): build graph · Primary build orchestration for engine, tools, and tests. · `BUILD.gn`
- [CMake Build](https://github.com/lynx-family/primjs/blob/develop/CMakeLists.txt): build system · Native/platform build integration used by addons and packages. · `CMakeLists.txt`
- [QuickJS Tests](https://github.com/lynx-family/primjs/tree/develop/testing/quickjs): engine tests · Debugger, GC, profiler, conformance, and engine behavior tests. · `testing/quickjs`
- [WASM Tests](https://github.com/lynx-family/primjs/tree/develop/testing/wasm): interop tests · WebAssembly API and runtime interoperability test suites. · `testing/wasm`

### Dependencies

Vendored or externally controlled code and public API surface.

- [Public API](https://github.com/lynx-family/primjs/tree/develop/include): headers · Stable embedders surface for C/C++ consumers. · `include`
- [wasm3](https://github.com/lynx-family/primjs/tree/develop/third_party/wasm3): vendored runtime · Vendored WASM runtime used by the interop subsystem. · `third_party/wasm3`
- [Test262 Patch](https://github.com/lynx-family/primjs/tree/develop/patches/test262): compat patch · Behavioral patch applied to upstream Test262 expectations. · `patches/test262`

## Connections

- QuickJS Core → GC System: allocates via · Engine object lifetime is governed by the GC boundary.
- PrimJS Runtime → QuickJS Core: extends · PrimJS layers platform/runtime integration on top of QuickJS.
- PrimJS Runtime → Public API: exports · Runtime-facing ABI is surfaced through public headers.
- GC System → Public API: publishes handles · GC-related public headers are part of the stable surface.
- Inspector → QuickJS Core: observes · Debugger and profilers consume runtime events and state.
- N-API Layer → QuickJS Core: adapts · Native addon support binds host APIs to the engine.
- N-API Layer → Public API: exposes · Addon-facing API is built on top of the public headers.
- WASM Bridge → QuickJS Core: binds · QuickJS-specific WASM bindings connect into the runtime.
- WASM Bridge → wasm3: wraps · Common WASM API can target vendored wasm3 runtime.
- QJS CLI → QuickJS Core: hosts · CLI is a thin executable around the engine runtime.
- Android → PrimJS Runtime: embeds · Android packaging ships the PrimJS engine and WASM glue.
- HarmonyOS → PrimJS Runtime: embeds · Harmony packaging builds and ships the engine integration.
- Apple Podspec → PrimJS Runtime: packages · Podspec distributes the embedded engine for Apple targets.
- GN Build → QuickJS Core: builds · GN drives the core engine compilation.
- GN Build → PrimJS Runtime: builds · GN also orchestrates PrimJS integration outputs.
- GN Build → Inspector: builds · Instrumentation components are included in the main build graph.
- GN Build → N-API Layer: builds · GN covers native addon compatibility layers.
- GN Build → WASM Bridge: builds · GN includes the WASM bridge and adapters.
- CMake Build → N-API Layer: supports · CMake is used by native addon and packaging integrations.
- CMake Build → Android: supports · Android native modules rely on CMake integration.
- QuickJS Tests → QuickJS Core: validates · Engine tests cover language, GC, debugger, and profiler behavior.
- QuickJS Tests → Inspector: validates · Debugger and profiler tests exercise the instrumentation layer.
- QuickJS Tests → GC System: validates · Dedicated GC tests exercise heap and liveness behavior.
- WASM Tests → WASM Bridge: validates · WASM suites cover interoperability and runtime adapters.
- WASM Tests → wasm3: exercises · Interop tests include coverage for wasm3-backed execution.
- Test262 Patch → QuickJS Tests: modifies · Test262 behavior is adjusted through local patches.
