# realcoldfry/tslua architecture

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

- Interactive diagram: https://gitdiagram.com/realcoldfry/tslua
- Repository: https://github.com/realcoldfry/tslua

## Overview

1. **Repository purpose and top-level boundary**
   - `tslua` is a TypeScript-to-Lua transpiler packaged as a single Go binary.
   - The core architectural boundary is between **TypeScript input + compiler state** on one side and **generated Lua output + support libraries** on the other.
   - It intentionally avoids a Node.js runtime in the compile path; TypeScript compiler logic is accessed from Go through `typescript-go` shims.

2. **Primary execution entry points**
   - `cmd/tslua` is the main CLI/compiler entry point.
   - `cmd/tslua-client` is a client used with the transpilation server/watch setup.
   - `cmd/luabench` runs Lua benchmark workloads for performance validation.
   - `cmd/wasm` builds the browser/WASM execution path used by the website playground.

3. **Compilation pipeline**
   - The main flow is: parse TypeScript project/config, resolve modules and options, build a TypeScript program, transform ASTs, then print Lua.
   - The `internal/transpiler` package is the central compiler layer. It owns language lowering for expressions, statements, classes, modules, destructuring, optional chaining, decorators, loops, numbers, JSX, and `using`.
   - The output is Lua AST rather than raw strings until the final print stage, which helps keep code generation structured.

4. **TypeScript compiler integration**
   - The `shim/*` directories are a major architectural seam. They wrap pieces of `typescript-go` such as parser, checker, scanner, compiler, incremental, VFS, and path logic.
   - These shims let the Go code call into TypeScript compiler internals directly, which is why the project can behave like a native compiler while still reusing TS semantics.
   - `patches/tsgo` and `extern/typescript-go` indicate the compiler dependency is vendored/modified and kept in lockstep by CI tooling.

5. **Lua code generation and AST**
   - `internal/lua` defines the Lua-side AST and printer.
   - `internal/emitpath` decides file/output path mapping for emitted Lua files.
   - `internal/sourcemap` handles source map generation so emitted Lua can be traced back to TypeScript sources.
   - This separation means the transpiler emits structured Lua, then a printer turns that into text with formatting and source mappings.

6. **Runtime support library (`lualib`)**
   - `internal/lualib` is the bundled runtime library used by generated code to emulate JavaScript/TypeScript behaviors in Lua.
   - It includes feature-specific Lua modules for arrays, promises, classes, iterators, objects, strings, symbols, weak collections, and source-map traceback support.
   - There are two library bundles: the general one and a Lua 5.0-compatible variant (`features_50`, `lualib_bundle_50.lua`), so target Lua version materially affects emitted support code.
   - `internal/lualibinfo` and `scripts/gen-lualib-features` manage metadata about which runtime helpers are needed.

7. **Target/runtime compatibility model**
   - The compiler targets multiple Lua runtimes: LuaJIT and Lua 5.0–5.5.
   - Compatibility is not just syntactic; it affects how features are lowered and which runtime helpers are bundled.
   - The repo also supports different class emission styles (`tstl`, `inline`, `luabind`, `middleclass`), so “class handling” is a configurable subsystem rather than a fixed code path.

8. **Server, watch, and eval tooling**
   - `cmd/tslua/server.go`, `watch.go`, and `eval.go` suggest the CLI can run in a long-lived server/watch mode for low-latency rebuilds and interactive evaluation.
   - `scripts/lua_eval_server*.lua` and the `tslua-client` binary indicate a two-process setup for evaluating code in a Lua runtime while keeping the compiler in Go.
   - This creates a clear split between the **compile service** and the **execution/evaluation host**, which is important for understanding data flow in watch/eval scenarios.

9. **Testing architecture**
   - There are two major test strategies:
     - `internal/luatest` runs a broad, Go-native compatibility suite against emitted Lua and runtime behavior.
     - `internal/tstltest` appears to mirror or migrate TypeScriptToLua’s original Jest-based tests into Go.
   - `scripts/migrate/*` is a specialized subsystem that extracts TSTL test cases, serializes them, and regenerates Go tests.
   - This means correctness validation is tied directly to TSTL parity, not just unit tests around small functions.

10. **Benchmarks and performance validation**
   - `internal/luabench` and `cmd/luabench` benchmark emitted Lua patterns such as array iteration, string concatenation, maps, sets, and numeric loops.
   - These benchmarks validate the runtime cost of generated code, which matters because transpilation correctness alone is not enough for this project.
   - The README’s emphasis on fast incremental rebuilds is backed by this performance-oriented subsystem.

11. **Examples and playground**
   - `examples/array-length-adapter` is a runnable sample project showing how consumer code is transpiled.
   - The `website` package is a separate documentation/playground application built with Astro.
   - The playground includes browser-side compilation and execution components, plus a WASM worker path, so users can interactively test transpilation without installing the CLI.

12. **Packaging and distribution**
   - The repo is split into Go source, npm packaging (`npm/tslua`), and website/docs hosting.
   - The npm package exposes the CLI for installation, while the Go binary is the actual compiler runtime.
   - GitHub Actions workflows cover CI, nightly builds, docs/pages publishing, and dependency/bump automation, so release flow is multi-target.

13. **External dependencies and boundaries**
   - `extern/tstl` and `extern/typescript-go` show that upstream TSTL tests and TypeScript compiler internals are treated as external dependencies.
   - The project uses GitHub Pages for documentation/playground hosting and Codecov for coverage tracking, but those are peripheral to the compile architecture.
   - The most important dependency boundary is the compiler-internal one: `tslua` reuses TypeScript compiler machinery, but owns the lowering, Lua runtime, and packaging layers.

14. **How to draw the architecture**
   - A useful diagram should center on `cmd/tslua` and `internal/transpiler` as the compile core.
   - Feed into that core: TypeScript source files, `tsconfig`, TypeScript compiler shims, and resolution/VFS services.
   - Feed out of that core: Lua AST/printer, sourcemaps, emitted Lua files, and optional bundled `lualib`.
   - Side branches should show watch/server/eval, benchmark/test harnesses, and the website/WASM playground as separate consumers of the same compiler core.

Main entry points: [`internal/transpiler/transpiler.go`](https://github.com/realcoldfry/tslua/blob/main/internal/transpiler/transpiler.go) (transpiler), [`cmd/tslua/main.go`](https://github.com/realcoldfry/tslua/blob/main/cmd/tslua/main.go) (tslua), [`website/src/pages/playground.astro`](https://github.com/realcoldfry/tslua/blob/main/website/src/pages/playground.astro) (playground), [`cmd/tslua/server.go`](https://github.com/realcoldfry/tslua/blob/main/cmd/tslua/server.go) (server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_cli["CLI & runtime"]
  node_cmd_tslua["tslua<br/>CLI compiler<br/>[main.go]"]
  node_cmd_tslua_server["server<br/>watch service<br/>[server.go]"]
  node_cmd_tslua_eval["eval<br/>runtime bridge<br/>[eval.go]"]
  node_cmd_tslua_client["client<br/>[main.go]"]
end

subgraph group_group_core["Compiler core"]
  node_internal_transpiler["transpiler<br/>lowering core<br/>[transpiler.go]"]
  node_ts_shims["ts shims<br/>typescript-go seam"]
  node_internal_resolve["resolve<br/>[resolve.go]"]
end

subgraph group_group_support["Support libs"]
  node_internal_emitpath["emit path<br/>output mapping<br/>[emitpath.go]"]
  node_internal_lua["Lua AST<br/>ast printer<br/>[ast.go]"]
  node_internal_sourcemap["sourcemaps<br/>mapping<br/>[sourcemap.go]"]
  node_internal_lualib["lualib<br/>runtime library<br/>[lualib.go]"]
  node_internal_lualibinfo["lualib info<br/>feature metadata<br/>[lualibinfo.go]"]
end

subgraph group_group_quality["Validation"]
  node_cmd_luabench["luabench<br/>benchmark CLI<br/>[main.go]"]
  node_internal_luatest["luatest<br/>compat test suite<br/>[luatest.go]"]
  node_internal_tstltest["tstltest<br/>parity tests"]
  node_scripts_migrate["migrate<br/>test migrator<br/>[migrate.ts]"]
end

subgraph group_group_playground["Playground & packaging"]
  node_cmd_wasm["wasm<br/>browser build<br/>[main.go]"]
  node_website_playground["playground<br/>docs app<br/>[playground.astro]"]
  node_npm_tslua["npm package<br/>distribution<br/>[package.json]"]
end

node_cmd_tslua -->|"orchestrates"| node_internal_transpiler
node_cmd_tslua -->|"loads config"| node_internal_resolve
node_cmd_tslua -->|"calls TS internals"| node_ts_shims
node_internal_transpiler -->|"emits AST"| node_internal_lua
node_internal_transpiler -->|"requests helpers"| node_internal_lualib
node_internal_transpiler -->|"consults features"| node_internal_lualibinfo
node_internal_transpiler -->|"names outputs"| node_internal_emitpath
node_internal_transpiler -->|"maps sources"| node_internal_sourcemap
node_internal_resolve -->|"uses VFS/path"| node_ts_shims
node_cmd_tslua_server -->|"serves client"| node_cmd_tslua_client
node_cmd_tslua_server -->|"eval flow"| node_cmd_tslua_eval
node_cmd_tslua_eval -->|"shared runtime"| node_cmd_wasm
node_cmd_wasm -->|"powers UI"| node_website_playground
node_website_playground -->|"compiles in browser"| node_internal_transpiler
node_website_playground -->|"executes Lua"| node_internal_lua
node_cmd_luabench -->|"benchmarks output"| node_internal_transpiler
node_internal_luatest -->|"verifies lowering"| node_internal_transpiler
node_internal_tstltest -->|"parity check"| node_internal_transpiler
node_scripts_migrate -->|"generates cases"| node_internal_tstltest
node_npm_tslua -->|"wraps binary"| node_cmd_tslua

click node_cmd_tslua "https://github.com/realcoldfry/tslua/blob/main/cmd/tslua/main.go"
click node_cmd_tslua_server "https://github.com/realcoldfry/tslua/blob/main/cmd/tslua/server.go"
click node_cmd_tslua_eval "https://github.com/realcoldfry/tslua/blob/main/cmd/tslua/eval.go"
click node_cmd_tslua_client "https://github.com/realcoldfry/tslua/blob/main/cmd/tslua-client/main.go"
click node_internal_transpiler "https://github.com/realcoldfry/tslua/blob/main/internal/transpiler/transpiler.go"
click node_ts_shims "https://github.com/realcoldfry/tslua/tree/main/shim"
click node_internal_resolve "https://github.com/realcoldfry/tslua/blob/main/internal/resolve/resolve.go"
click node_internal_emitpath "https://github.com/realcoldfry/tslua/blob/main/internal/emitpath/emitpath.go"
click node_internal_lua "https://github.com/realcoldfry/tslua/blob/main/internal/lua/ast.go"
click node_internal_sourcemap "https://github.com/realcoldfry/tslua/blob/main/internal/sourcemap/sourcemap.go"
click node_internal_lualib "https://github.com/realcoldfry/tslua/blob/main/internal/lualib/lualib.go"
click node_internal_lualibinfo "https://github.com/realcoldfry/tslua/blob/main/internal/lualibinfo/lualibinfo.go"
click node_cmd_luabench "https://github.com/realcoldfry/tslua/blob/main/cmd/luabench/main.go"
click node_internal_luatest "https://github.com/realcoldfry/tslua/blob/main/internal/luatest/luatest.go"
click node_internal_tstltest "https://github.com/realcoldfry/tslua/tree/main/internal/tstltest"
click node_scripts_migrate "https://github.com/realcoldfry/tslua/blob/main/scripts/migrate/migrate.ts"
click node_cmd_wasm "https://github.com/realcoldfry/tslua/blob/main/cmd/wasm/main.go"
click node_website_playground "https://github.com/realcoldfry/tslua/blob/main/website/src/pages/playground.astro"
click node_npm_tslua "https://github.com/realcoldfry/tslua/blob/main/npm/tslua/package.json"

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_cmd_tslua,node_cmd_tslua_server,node_cmd_tslua_eval,node_cmd_tslua_client toneBlue
class node_internal_transpiler,node_ts_shims,node_internal_resolve toneAmber
class node_internal_emitpath,node_internal_lua,node_internal_sourcemap,node_internal_lualib,node_internal_lualibinfo toneMint
class node_cmd_luabench,node_internal_luatest,node_internal_tstltest,node_scripts_migrate toneRose
class node_cmd_wasm,node_website_playground,node_npm_tslua toneIndigo
```

## Components

### CLI & runtime

Go entrypoints and long-lived compiler/eval processes.

- [tslua](https://github.com/realcoldfry/tslua/blob/main/cmd/tslua/main.go): CLI compiler · Main command-line compiler and orchestration entrypoint. · `cmd/tslua/main.go`
- [server](https://github.com/realcoldfry/tslua/blob/main/cmd/tslua/server.go): watch service · Long-lived build/watch and evaluation server logic. · `cmd/tslua/server.go`
- [eval](https://github.com/realcoldfry/tslua/blob/main/cmd/tslua/eval.go): runtime bridge · Interactive evaluation path used with the Lua host. · `cmd/tslua/eval.go`
- [client](https://github.com/realcoldfry/tslua/blob/main/cmd/tslua-client/main.go): eval client · Client binary for server/watch transpilation workflows. · `cmd/tslua-client/main.go`

### Compiler core

TypeScript ingestion, lowering, output, and compatibility boundaries.

- [transpiler](https://github.com/realcoldfry/tslua/blob/main/internal/transpiler/transpiler.go): lowering core · Central compiler layer that lowers TypeScript into Lua AST. · `internal/transpiler/transpiler.go`
- [ts shims](https://github.com/realcoldfry/tslua/tree/main/shim): typescript-go seam · Shim layer for parser, checker, compiler, VFS, scanner, and path integration. · `shim`
- [resolve](https://github.com/realcoldfry/tslua/blob/main/internal/resolve/resolve.go): module resolver · Module and path resolution logic for TypeScript inputs. · `internal/resolve/resolve.go`

### Support libs

Lua AST, output mapping, sourcemaps, and bundled runtime helpers.

- [emit path](https://github.com/realcoldfry/tslua/blob/main/internal/emitpath/emitpath.go): output mapping · Maps source files and project structure to emitted Lua paths. · `internal/emitpath/emitpath.go`
- [Lua AST](https://github.com/realcoldfry/tslua/blob/main/internal/lua/ast.go): ast printer · Structured Lua AST and final printer for emitted text. · `internal/lua/ast.go`
- [sourcemaps](https://github.com/realcoldfry/tslua/blob/main/internal/sourcemap/sourcemap.go): mapping · Source map generation and traceability support for emitted Lua. · `internal/sourcemap/sourcemap.go`
- [lualib](https://github.com/realcoldfry/tslua/blob/main/internal/lualib/lualib.go): runtime library · Bundled JavaScript/TypeScript compatibility helpers for Lua targets. · `internal/lualib/lualib.go`
- [lualib info](https://github.com/realcoldfry/tslua/blob/main/internal/lualibinfo/lualibinfo.go): feature metadata · Tracks runtime helper feature metadata and bundle requirements. · `internal/lualibinfo/lualibinfo.go`

### Validation

Tests, benchmarks, and migration tooling that lock behavior to TSTL parity.

- [luabench](https://github.com/realcoldfry/tslua/blob/main/cmd/luabench/main.go): benchmark CLI · Benchmark entrypoint for emitted Lua performance validation. · `cmd/luabench/main.go`
- [luatest](https://github.com/realcoldfry/tslua/blob/main/internal/luatest/luatest.go): compat test suite · Go-native compatibility suite over emitted Lua and runtime behavior. · `internal/luatest/luatest.go`
- [tstltest](https://github.com/realcoldfry/tslua/tree/main/internal/tstltest): parity tests · Ported TypeScriptToLua test corpus for behavioral parity checks. · `internal/tstltest`
- [migrate](https://github.com/realcoldfry/tslua/blob/main/scripts/migrate/migrate.ts): test migrator · Extracts, serializes, and regenerates parity tests from upstream TSTL. · `scripts/migrate/migrate.ts`

### Playground & packaging

WASM/browser path, docs site, and distributable packaging.

- [wasm](https://github.com/realcoldfry/tslua/blob/main/cmd/wasm/main.go): browser build · WASM/browser build entrypoint for the playground execution path. · `cmd/wasm/main.go`
- [playground](https://github.com/realcoldfry/tslua/blob/main/website/src/pages/playground.astro): docs app · Astro-based docs site and interactive playground UI. · `website/src/pages/playground.astro`
- [npm package](https://github.com/realcoldfry/tslua/blob/main/npm/tslua/package.json): distribution · Published npm wrapper that exposes the CLI for installation. · `npm/tslua/package.json`

## Connections

- tslua → transpiler: orchestrates · CLI drives compilation through the transpiler core.
- tslua → resolve: loads config · CLI parses project settings and resolves sources.
- tslua → ts shims: calls TS internals · Compiler path uses typescript-go shims instead of Node.js runtime.
- transpiler → Lua AST: emits AST · Transpiler produces structured Lua AST before final printing.
- transpiler → lualib: requests helpers · Lowering selects runtime support modules as needed.
- transpiler → lualib info: consults features · Feature metadata determines which helpers are bundled.
- transpiler → emit path: names outputs · Emission is paired with path mapping for generated files.
- transpiler → sourcemaps: maps sources · Lowering and printing preserve source locations for maps.
- resolve → ts shims: uses VFS/path · Resolution depends on TS-compatible filesystem and path behavior.
- server → client: serves client · Watch/server mode is paired with the client binary.
- server → eval: eval flow · Server path supports interactive evaluation against Lua runtime.
- eval → wasm: shared runtime · Evaluation concepts are reused in the browser/WASM execution path.
- wasm → playground: powers UI · WASM build feeds the interactive playground.
- playground → transpiler: compiles in browser · Playground compiles TypeScript using the same compiler core.
- playground → Lua AST: executes Lua · Browser-side output is run and displayed through Lua execution helpers.
- luabench → transpiler: benchmarks output · Benchmarks measure performance of generated Lua patterns.
- luatest → transpiler: verifies lowering · Compatibility tests exercise compiler behavior end to end.
- tstltest → transpiler: parity check · Upstream-inspired tests validate behavior against TSTL expectations.
- migrate → tstltest: generates cases · Migration tooling extracts and regenerates parity test fixtures.
- npm package → tslua: wraps binary · npm packaging exposes the Go CLI to users.
