Skip to content

Lua: shared allocator and init error handler, class metadata only when read - #1493

Merged
Frotty merged 3 commits into
masterfrom
lua-emission-shared-helpers
Oct 18, 2026
Merged

Frotty merged 3 commits into
masterfrom
lua-emission-shared-helpers

Conversation

@Frotty

@Frotty Frotty commented Oct 18, 2026

Copy link
Copy Markdown
Member

Three changes to what the Lua backend emits. None changes what a Wurst program does; each removes code the script carried for every class or every package.

Changes

  1. One error handler for all package inits. Each package init passed its own copy of the same two-closure handler to xpcall. It is now one function, __wurst_init_error, passed by name. Its text is unchanged.
  2. One shared allocator. Every class had a create method with its own copy of the allocator. A class whose allocation only takes an id and records its class (no array field needs its table, no field default survived at an allocation) now has none. Its allocations call __wurst_allocObject(C), a main-chunk local like __wurst_deallocObject. A class which writes fields on allocation keeps its own create, so it gains no extra call.
  3. Class metadata only when it is read. Every class table got a __wurst_supertypes table and a __typeId__. Only instanceof and the type id of an object read them. The translator now looks for those two reads (ImInstanceof, ImTypeIdOfObj, both made before Lua emission) and emits the supertypes (with isInstanceOf) and the type ids only when the program has them.

Only the three Lua translation files change, so Jass output is not affected.

Effect on two large maps

Built with -lua -inline -localOptimizations:

before after
Map A script 8.52 MB, 482,277 instructions 7.92 MB (−7.0%), 433,739 instructions (−10.1%)
Map B script 5.34 MB 4.93 MB (−7.7%)

On map A, 1,140 fewer tables are built at init. Map A reads no class metadata, so all of it goes. Map B uses instanceof, so it keeps its supertypes; it reads no object type id, so its type ids go.

For both maps, the old output was rewritten the way the change should rewrite it: shared-allocator classes lose create and their calls become __wurst_allocObject(C), the inline handlers become the shared one, and unread metadata goes. After that rewrite, the only remaining differences from the new output are the two new helper definitions and renumbered createN names (plus a build timestamp). luac accepts both scripts.

Verification

  • New tests: packageInitsShareOneErrorHandler, classesWhichOnlyAllocateShareOneAllocator (allocation, destroy, id reuse, dispatch and array fields at run time), classMetadataIsEmittedOnlyForTheReadsWhichNeedIt (with and without instanceof and type ids, at run time).
  • Tests that checked the old shape now check the new one: allocation sites are __wurst_allocObject(C). Three tests whose subject is a create method or the metadata placement got programs that need them (an array field, an instanceof, an object type id).
  • Each commit was tested on its own: LuaBackendAuditTests, LuaTranslationTests, ObservableEffectsTests, FastHashMapTests, TypeClassTests, DeterministicChecks, FieldIterationTests, LuaKeyedMapTests, ClassesTests, NewFeatureTests, GenericsTests, GenericsWithTypeclassesTests, CompiletimeTests pass locally (Windows). The full suite and the other platforms are left to CI.
  • In game, measured on the 3.0.0 client: both maps, built with the old and the new compiler, load, initialise every package and start running with no uncaught error (four passes). This covers loading and package initialisation, not gameplay.

Behaviour note

Raw Lua bundled with a map which reads a class table's create, __wurst_supertypes or __typeId__ would no longer find them where they are not needed. These are internal to the emitted script, and foreign code touching emitted structures is outside what the backend supports.

Each package init passed its own copy of the same two-closure error
handler to xpcall. The handler is now one function, __wurst_init_error,
defined once and passed by name; its text is unchanged.
Every class got a create method holding its own copy of the allocator.
A class whose allocation only takes an id and records its class (no
array field to make a table for, no default which survived at an
allocation) now has none: its allocations call __wurst_allocObject(C),
a main-chunk local like __wurst_deallocObject. A class which writes
fields on allocation keeps its own create, so it gains no extra call.
Every class table got a __wurst_supertypes table and a __typeId__,
read only by instanceof and by the type id of an object. The
translator now looks for those two reads in the program and emits the
supertypes (with isInstanceOf) and the type ids only when it has them.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T10:44:55.662065Z 9d86505 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Frotty
Frotty merged commit 998a05b into master Oct 18, 2026
2 checks passed
Frotty added a commit that referenced this pull request Oct 20, 2026
A compile-time object kept its id on Jass only. The Lua compile-time state
allocated each object from the shared allocator, so it got the next free id,
and a compile-time value which had captured the id (castTo int, a stored id,
an old-generics slot) pointed at another object or at none.

The compile-time interpreter now hands out ids from one space on the Lua
target, as Lua does, and the Lua compile-time state replays them the way the
Jass one does: before each gap it empties the free list and sets the next id
(__wurst_resumeObjectIds, printed as the two assignments to the allocator's
chunk locals), and after the last object it continues after the highest id
used at compile time. Jass output is unchanged.

Present since class instances became integer ids on Lua (#1264), not from
the shared allocator (#1493): the replay always set Jass's per-class
maxIndex, which Lua never reads.
@Frotty
Frotty deleted the lua-emission-shared-helpers branch November 25, 2026 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant