How lynx-family/primjs works
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: interpreter/quickjs QuickJS Core · primjs PrimJS Runtime · BUILD.gn GN Build · testing/quickjs QuickJS Tests
Components 17
Core Engine
QuickJS-derived runtime plus PrimJS integration and GC boundary.
- QuickJS Core · Language runtime, parser/compiler support, libc helpers, and versioning.
src/interpreter/quickjs - PrimJS Runtime · PrimJS-specific runtime integration and snapshot/build support.
src/interpreter/primjs - GC System · Allocator, page layout, marking/tracing, handles, and GC logging.
src/gc
Extensions
Developer tooling and host integration layers that sit beside the engine.
- Inspector · Debugger, CPU profiler, heap profiler, and protocol/runtime support.
src/inspector - N-API Layer · Host API abstraction with engine adapters and environment/runtime management.
src/napi - WASM Bridge · Common WASM API with engine- and runtime-specific adapters.
src/wasm - QJS CLI · Standalone command-line entry point for the engine.
tools/qjs-cli
Platform Packaging
Platform-specific embedding and distribution targets.
- Android · Android app/module packaging and native registration glue.
Android - HarmonyOS · Harmony app/module packaging and build scripts.
harmony - Apple Podspec · CocoaPods packaging for Apple distribution.
PrimJS.podspec
Quality & Validation
Tests, conformance, and CI automation.
- GN Build · Primary build orchestration for engine, tools, and tests.
BUILD.gn - CMake Build · Native/platform build integration used by addons and packages.
CMakeLists.txt - QuickJS Tests · Debugger, GC, profiler, conformance, and engine behavior tests.
testing/quickjs - WASM Tests · WebAssembly API and runtime interoperability test suites.
testing/wasm
Dependencies
Vendored or externally controlled code and public API surface.
- Public API · Stable embedders surface for C/C++ consumers.
include - wasm3 · Vendored WASM runtime used by the interop subsystem.
third_party/wasm3 - Test262 Patch · Behavioral patch applied to upstream Test262 expectations.
patches/test262
Connections 26
- 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.