Repository navigation
Lua: implement the Reflection natives - #1508
Conversation
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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 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.
|
@codex review |
There was a problem hiding this comment.
💡 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).
|
@codex review |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
typeIdToTypeName,maxTypeId,instanceCountandmaxInstanceCount(packageReflection) were implemented only byEliminateClasseson Jass. On Lua they compiled to generic native stubs answering""or 0 without a diagnostic, soNewFeatureTests.typeName,typeName2,maxTypeId,instanceCountandinstanceMaxCountheld only withtestLua(false)(BACKLOG.md).Semantics matched (from
EliminateClasses)maxTypeIdis the largest, -1 without classes;typeIdToTypeNameis"Package.Class".instanceCountcounts 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;maxInstanceCountis 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 intoLuaTranslatorandRemoveGarbage:C.typeIdor 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.instanceCountormaxInstanceCount, and only for the groups it can ask about; the peak only for groupsmaxInstanceCountasks about. Counted groups get their own deallocation function; everything else keeps the shared allocator.maxTypeIdor passes a run-time id keeps every class, so the numbering matches Jass.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
unreachedReflectionLeavesTheLuaUnchangedasserts identical Lua with and without unreached calls, with and without-opt.Documented differences (docs/WC3_RUNTIME.md, pinned in tests)
"unknown"and 0. The Jass behaviour is a new BACKLOG.md entry.Box<int>,Sub<int>,Box<string>:maxTypeId3, two groups); Lua keeps one erased class per source class (maxTypeId2, one group counting all three). Matching would need a class per instantiation, which is the general Lua monomorphisation AGENTS.md §9 rules out.newGenericInstantiationsAreOneClassOnLuapins 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.