Skip to content

Lua: implement the Reflection natives - #1508

Merged
Frotty merged 5 commits into
masterfrom
lua-type-id-functions
Oct 20, 2026
Merged

Frotty merged 5 commits into
masterfrom
lua-type-id-functions

Conversation

@Frotty

@Frotty Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member

typeIdToTypeName, maxTypeId, instanceCount and maxInstanceCount (package Reflection) were implemented only by EliminateClasses on Jass. On Lua they compiled to generic native stubs answering "" or 0 without a diagnostic, so NewFeatureTests.typeName, typeName2, maxTypeId, instanceCount and instanceMaxCount held only with testLua(false) (BACKLOG.md).

Semantics matched (from EliminateClasses)

  • Type ids number from 1 in package, name and source order, each class followed by its subclasses; maxTypeId is the largest, -1 without classes; typeIdToTypeName is "Package.Class".
  • instanceCount counts the live objects of the class's whole allocator group (every class joined to it by inheritance: one allocator on Jass), not the class alone; maxInstanceCount is that group's peak of live objects, which on Jass is its highest id. Lua tracks both exactly.

What Lua emits, and only when

New LuaReflection, wired into LuaTranslator and RemoveGarbage:

  • Calls are printed where they are made: a known id (C.typeId or a literal) becomes a string literal, an integer or a single counter read; an id known only at run time reads a table of names or groups.
  • Live counters are emitted only when the program calls instanceCount or maxInstanceCount, and only for the groups it can ask about; the peak only for groups maxInstanceCount asks about. Counted groups get their own deallocation function; everything else keeps the shared allocator.
  • A program that calls maxTypeId or passes a run-time id keeps every class, so the numbering matches Jass.
  • Using one of these natives as a function reference is a compile error on Lua (it has no definition, as on Jass, where only its calls are replaced).

A program that never reaches these natives emits byte-identical Lua: 1,123 emitted files of the touched classes (281 Lua, 842 Jass) match master, and unreachedReflectionLeavesTheLuaUnchanged asserts identical Lua with and without unreached calls, with and without -opt.

Documented differences (docs/WC3_RUNTIME.md, pinned in tests)

  • Out-of-range ids: Jass's search has no bounds, so an id below 1 or above the maximum answers for the first or last class; Lua answers "unknown" and 0. The Jass behaviour is a new BACKLOG.md entry.
  • Compile-time objects: Jass counts the ids of objects destroyed or dropped at compile time as live (5 against Lua's 2 in the pinned test).
  • New-generic classes: Jass specialises each instantiation into its own class and type id (Box<int>, Sub<int>, Box<string>: maxTypeId 3, two groups); Lua keeps one erased class per source class (maxTypeId 2, one group counting all three). Matching would need a class per instantiation, which is the general Lua monomorphisation AGENTS.md §9 rules out. newGenericInstantiationsAreOneClassOnLua pins each backend's listing.

The IM interpreter counts the exact class only, disagreeing with both backends: new BACKLOG.md entry.

Tests

Passing locally: NewFeatureTests 42 (the five now also run on Lua), LuaTranslationTests 151, LuaBackendAuditTests 243, ClassesTests 128, DeterministicChecks 28.

Tested on Windows with the bundled Lua 5.3; Linux runs in PR CI, macOS only on master.

typeIdToTypeName, maxTypeId, instanceCount and maxInstanceCount compiled
to the generic native stubs on Lua and answered "" or 0 without a
diagnostic. They now answer what EliminateClasses answers on Jass: the
qualified name of a type id's class, the largest type id (-1 without a
class), and the live objects and the peak of the class's allocator
group, every class joined to it by inheritance (one allocator on Jass),
not of the class alone. maxInstanceCount is the group's high-water mark,
which on Jass is its highest id.

A call is printed in place (LuaReflection). A type id known at compile
time (C.typeId or a literal) folds to a name, a literal or one counter
read; one known only at run time reads a table of names or of groups.
Lua has one allocator for every class, so a group's live count and peak
are counters which the allocators and destroys of its classes update:
only in a program which calls these natives, only for the groups it can
ask about, and the peak only for maxInstanceCount. A counted class gets
its own allocator, and its destroy goes through a counting copy of the
shared deallocator, which finds the group in the class table. A program
which does not reach the natives emits the same Lua as before; the Lua
and Jass of every existing test in the touched classes are unchanged.

Lua numbers the classes the emitted program keeps, so a program which
can see the numbering (maxTypeId, or a type id which is not C.typeId or
o.typeId) keeps every class, as Jass numbers the unused ones too. A
function reference to one of the natives is now a Lua backend error
instead of a reference to a function nobody defines.

Two differences remain and are pinned in tests and documented in
docs/WC3_RUNTIME.md: Jass searches the type ids without bounds, so a
number below 1 or above maxTypeId answers for the first or last class
where Lua answers "unknown" and 0; and the Jass replay of compile-time
objects counts the ids of objects destroyed or dropped at compile time as
live, while Lua counts the objects it makes again. The first, and the
interpreter's per-class counting, are in BACKLOG.md.
Jass specialises Box<int>, Sub<int> and Box<string> into three classes with their own type ids and two allocator groups; Lua keeps one erased Box and Sub, so it has two type ids and counts all three objects in one group. The names agree. Matching Jass would take a class per instantiation on Lua, or the type arguments at every allocation, which erased generic code does not have, so the difference is documented in docs/WC3_RUNTIME.md and pinned for both backends.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 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-20T17:58:46.432217Z d1efb58 Manual request
ℹ️ 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 commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7cbd7a80f5

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

A targeted specialisation kept the class it was copied from, and both got a type id, so maxTypeId and the reflection tables listed the class twice while its objects carried the original's id. The Lua backend now numbers only the classes which are not copies; a copy has the type id and the counted group of its class, for C.typeId, the type id objects carry and the Reflection natives alike. A tuple type argument, which forces such a copy, now lists Box once on Lua, as on Jass.
@Frotty

Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 964ea3131c

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

instanceof Box<int> makes the Lua backend point Sub's superclass edge at the Box<int> copy. The type ids counted only classes which are not copies but followed the superclass edges as they are, so Sub, whose superclass was a copy, was never reached: it had no type id, and reading one threw. The numbering and the Reflection groups now follow the superclass edges to the classes the copies were made from (TypeId.calculate takes the edges; Jass numbers as before).
@Frotty

Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: d1efb584b2

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Frotty
Frotty merged commit 0a25e1d into master Oct 20, 2026
2 checks passed
Frotty added a commit that referenced this pull request Oct 20, 2026
 made stale, exempt the Jass halves of the Reflection difference tests
@Frotty
Frotty deleted the lua-type-id-functions 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