How realcoldfry/tslua works
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: transpiler.go transpiler · main.go tslua · playground.astro playground · server.go server
Components 19
CLI & runtime
Go entrypoints and long-lived compiler/eval processes.
- tslua · Main command-line compiler and orchestration entrypoint.
cmd/tslua/main.go - server · Long-lived build/watch and evaluation server logic.
cmd/tslua/server.go - eval · Interactive evaluation path used with the Lua host.
cmd/tslua/eval.go - client · Client binary for server/watch transpilation workflows.
cmd/tslua-client/main.go
Compiler core
TypeScript ingestion, lowering, output, and compatibility boundaries.
- transpiler · Central compiler layer that lowers TypeScript into Lua AST.
internal/transpiler/transpiler.go - ts shims · Shim layer for parser, checker, compiler, VFS, scanner, and path integration.
shim - resolve · 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 · Maps source files and project structure to emitted Lua paths.
internal/emitpath/emitpath.go - Lua AST · Structured Lua AST and final printer for emitted text.
internal/lua/ast.go - sourcemaps · Source map generation and traceability support for emitted Lua.
internal/sourcemap/sourcemap.go - lualib · Bundled JavaScript/TypeScript compatibility helpers for Lua targets.
internal/lualib/lualib.go - lualib info · 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 · Benchmark entrypoint for emitted Lua performance validation.
cmd/luabench/main.go - luatest · Go-native compatibility suite over emitted Lua and runtime behavior.
internal/luatest/luatest.go - tstltest · Ported TypeScriptToLua test corpus for behavioral parity checks.
internal/tstltest - migrate · Extracts, serializes, and regenerates parity tests from upstream TSTL.
scripts/migrate/migrate.ts
Playground & packaging
WASM/browser path, docs site, and distributable packaging.
- wasm · WASM/browser build entrypoint for the playground execution path.
cmd/wasm/main.go - playground · Astro-based docs site and interactive playground UI.
website/src/pages/playground.astro - npm package · Published npm wrapper that exposes the CLI for installation.
npm/tslua/package.json
Connections 20
- 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.