Repository navigation
Agent docs: Warcraft III runtime facts and corrected test guidance #1370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6eafe4b
Test that emitted Lua uses only libraries the game provides; fix the …
Frotty 22e34cb
Agent docs: Warcraft III runtime facts, sibling projects, corrected t…
Frotty 30a3e82
Merge remote-tracking branch 'origin/master' into claude/repo-doc-age…
Frotty 8aa6d77
Agent docs: toolchain ownership, determinism, two generics, performan…
Frotty 8152dde
Guard every Lua native fallback, not just the ones a fixture emits
Frotty e18946d
Scan the Lua backend's literals instead of enumerating helper sources
Frotty ded2394
Docs: Jass object ids are shared across an inheritance group, not per…
Frotty 50f8efa
Docs: only non-__wurst_ native fallbacks are guarded by 'not yet defi…
Frotty 1c80641
Docs and guard: say only what is verified about the test Lua, stack t…
Frotty ad19b84
Scope the os.exit exemption to the testSuccess registration
Frotty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| @AGENTS.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Warcraft III runtime facts for compiler work | ||
|
|
||
| What the game does that the compiler, the optimiser and the interpreter mocks have to respect. The game is the specification. The places a reader would look instead (Blizzard.j, the IM interpreter, the Lua test runtime) each differ from it, so every fact here says how it is known: | ||
|
|
||
| * **measured**: observed in the real client, version given. Do not overturn one by reading code; measure again. | ||
| * **code**: follows from what this repository emits or does; the pointer is the evidence. | ||
| * **reported**: community documentation, not measured here. Likely, not proven. | ||
|
|
||
| The rules that follow are in [AGENTS.md](../AGENTS.md) §7 and §11. | ||
|
|
||
| ## Three places code runs, one of them is the game | ||
|
|
||
| | | IM interpreter (compile time, `executeProg()`) | Test Lua (`testLua(true).executeProg()`) | The game | | ||
| |---|---|---|---| | ||
| | Reals | Java `float`, round to nearest (code: `ILconstReal`) | Lua doubles (a stock Lua: 5.3 on Windows and the Linux CI, unchecked elsewhere) | Jass: 32-bit, literals not correctly rounded. Lua: 24-bit mantissa, arithmetic truncates (measured, below) | | ||
| | Lua libraries | none | every standard library of that interpreter; `os.exit()` is how `testSuccess` ends a run (code: `LuaNatives`) | Lua 5.3 without some of them (reported, below) | | ||
| | Natives | mocks in `wurstio/jassinterpreter/providers` | `wc3shim.lua` plus the Reforged `common.j`/`blizzard.j` Lua dumps (`src/test/resources/luaruntime/README.md`) | the engine | | ||
|
|
||
| A test that passes in the first two columns says nothing about the third where they differ. The natives `LuaNatives` defines for a Jass native name (such as `GetRandomInt`) are fallbacks: the script defines one only if the game has not, so the real implementation wins (`LuaTranslator.translateFunc`). The `__wurst_`-prefixed ones are Wurst's own, never pre-defined by the game, and emitted without that guard. | ||
|
|
||
| ## Objects and memory | ||
|
|
||
| * **code** Wurst has no garbage collector. A class instance is an integer id on both targets. `new` takes an id, `destroy` runs `ondestroy` and gives the id back, and a second `destroy` is an error (`Double free: object of type X` on Jass, `Double free or invalid Wurst object.` on Lua). A closure is a class instance too and keeps its id until destroyed. (`RecycleCodeGeneratorQueue`, `LuaTranslator.createObjectManagement`.) | ||
| * **code** Both targets reuse the most recently freed id first (a stack, despite the name `RecycleCodeGeneratorQueue`), so `destroy` followed by `new` from the same allocator returns the same id and a stale reference then reads the new object. On Jass each group of classes joined by inheritance (a class with its `superClasses`, `ImTranslator.buildClassPartitions`) has one allocator: a subclass, its siblings and the base class share ids and the cap, so a stale id may come back as an object of a related class. The group runs out at `JASS_MAX_ARRAY_SIZE` live instances (default `Constants.MAX_ARRAY_SIZE`, 32768) with `Out of memory: Could not create X.`. On Lua there is one id space shared by every class and no cap, so a stale id may come back as an object of any class. | ||
| * **code** Lua keeps one static table per field, indexed by id, and a class descriptor per live id. `destroy` removes the descriptor and recycles the id; field storage is not cleared, as Jass arrays are not. Until the id is reused a stale reference fails virtual dispatch and `instanceof` is false. Lua's collector therefore frees no Wurst object, and a table stored in a field stays reachable after `destroy`. | ||
|
|
||
| ## The game's Lua | ||
|
|
||
| * **reported** It is Lua 5.3, with `debug` disabled ([Hive guide](https://www.hiveworkshop.com/threads/a-comprehensive-guide-to-mapping-in-lua.341880/)) and `collectgarbage` inaccessible since a patch, "it could cause desync" ([forum post](https://www.hiveworkshop.com/threads/wc3-reforged-lua.326354/)). The repository already treats `debug.traceback` as unavailable (`LuaTranslationTests.luaErrorWrappersAvoidUnavailableNativeTraceback`). Nothing in emitted code needs `io`, `os`, `package` or `require`; they are not known to exist there. `LuaTranslationTests.emittedLuaUsesOnlyLibrariesTheGameProvides` fails if a program's emitted helpers reach for any of these. `noLuaBackendLiteralNamesALibraryTheGameWithholds` scans the string literals under `translation/` (bar two files that only list names), because natives and fallback helpers are emitted only on demand. A name counts anywhere in the text, inside a Lua string or comment too, since a string can be run with `load`. Only the test-only `testSuccess` is exempt: it ends a test run with `os.exit`. | ||
| * **code** `debug.traceback` is not used (above), so `-stacktraces` keeps its own call stack in the script (`StackTraceInjector2`: `wurst_stack`, `wurst_stack_depth`). Each call of an instrumented function pays for it: an extra position argument at the call site, a push and a pop in the callee. `main` and `config` are skipped. `wurst_run.args` defaults to `-stacktraces` without `-inline` (`WurstCommands.defaultArgs`). | ||
| * **code** A function reference (a `code` value, such as one handed to a native) gets an `xpcall` adapter (`LuaTranslator.callbackAdapterFor`) and package init is `xpcall`-wrapped, so an error is reported through the Wurst error function when the program has one, else through `BJDebugMsg`. | ||
| * **code** The reference Lua 5.3 neither inlines a local function call nor caches a global or library lookup: `luac -l` on a loop that calls a one-line local function and reads a global and `math.floor` shows a `CALL` for the function and a `GETTABUP`/`SETTABUP` or `GETTABUP`+`GETTABLE` for each access, every iteration. If the game's VM is the reference 5.3 (reported above), every helper call, global read, table index and allocation the compiler leaves in runs, and the compiler's inliner and local optimisations are the only optimiser. | ||
| * **code** Jass has no `throw`. `I2S(1 div 0)` is the deliberate thread abort (`ErrorHandling.error()` ends with it); Lua lowers exactly that shape to `error("__wurst_abort_thread", 0)`, which the callback handlers ignore (`ExprTranslation.isIntentionalThreadAbortCall`, `LuaNativeLowering`). Folding or reshaping it breaks the abort (`LuaBackendAuditTests`, `OptimizerTests`). | ||
|
|
||
| ## Lockstep | ||
|
|
||
| Every client simulates the same game from the same inputs; anything that makes the simulation differ between machines desyncs it. | ||
|
|
||
| * **reported** The iteration order of `pairs()` is not the same for different players ([SyncedTable](https://www.hiveworkshop.com/threads/syncedtable.332894/)). So emitted Lua and stdlib code that compiles to Lua never iterate a table whose order is observable. A table is safe as a membership set (`t[k] = true`, `t[k] ~= nil`, `t[k] = nil`); anything iterated needs a separately kept insertion-ordered array, which is what a sparse set's dense array buys. | ||
| * **code** The keyed table offers no iteration (`LuaNatives`). `__wurst_keyedMapDestroy` does loop with `pairs`, but only to set every entry to nil, which does not depend on order. | ||
| * **code** The compiler's own output must be byte-identical for identical input (AGENTS.md §3). | ||
|
|
||
| ## Numbers | ||
|
|
||
| * **measured, 3.0.0** Jass literals and `S2R` are not correctly rounded and the two disagree: `0.1` reads one float high, `1.1` one float low, `S2R("100.0")` one above 100. Short exact binary fractions (`0.5`, `0.75`, `100.0`, `123456.78125`) read exactly; integer parts past 2^24 are truncated. This is why `SimpleRewrites.foldRealExactly` folds only exact literals and exact results. | ||
| * **measured, 3.0.0** Lua reals have a 24-bit mantissa, not 53: `(2^24 + 1) - 2^24` is 0, the literal `16777217.` reads as 16777216, `0.9999999999999999` reads as exactly 1, and `0.7 + 0.1 + 0.1 + 0.1` computes to 1 - 2^-23. Literals are rounded to nearest and each arithmetic result is truncated, which neither double nor 32-bit float arithmetic reproduces. One `R2I` result is not evidence of a precision: `R2I(0.7 + 0.1 + 0.1 + 0.1)` is 0 for that reason, not because of doubles. | ||
| * **measured, 3.0.0** `R2I` and the arithmetic `x // 1 | 0` agree on every value probed, the 32-bit edges and values outside the range included (`LuaNativeLowering.lowerRealToInt`). | ||
|
|
||
| ## Establishing a fact | ||
|
|
||
| * Measure in the real client with discriminating values, such as `2^24 + 1` or sums just below an integer, computed at run time from array operands so that nothing is folded, and read the emitted Lua to confirm. State the client version where the fact is recorded. | ||
| * Never launch Warcraft III yourself: it needs the maintainer's Battle.net sign-in and an install that matches the pinned patch. Ask for a probe. | ||
| * Benchmark Lua output like a shipping map: `-inline -localOptimizations` and no `-stacktraces`, which inflated call-heavy blocks 3-4x. Accumulate inside a timed loop in a local, not a global: a global's lookup cost follows the global table's hash layout, which shifts when identifiers are renamed, so a block whose Lua is byte-identical moved 15% between builds. Compare alternating runs, at least three per build, and treat shifts under 15% in global-touching blocks as noise. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.