Skip to content

Lua: keep the ids of compile-time objects - #1504

Merged
Frotty merged 2 commits into
masterfrom
claude/dazzling-ellis-e2cbb6
Oct 20, 2026
Merged

Frotty merged 2 commits into
masterfrom
claude/dazzling-ellis-e2cbb6

Conversation

@Frotty

@Frotty Frotty commented Oct 20, 2026

Copy link
Copy Markdown
Member

On Lua, an object made at compile time got a new id at run time, so a compile-time value which had captured its id (castTo int, a stored id, an old-generics slot) pointed at another object or at none. Jass kept the id.

Repro (interpreter and Jass pass, Lua failed):

class A
    int storedId
@compiletime function build() returns A
    new A
    let keep = new A
    keep.storedId = keep castTo int
    return keep
let kept = compiletime(build())
init
    if kept castTo int == kept.storedId
        testSuccess()

Cause

CompiletimeFunctionRunner.emitCompiletimeObjectAllocs replays each compile-time allocation by setting the class group's maxIndex before it. Lua never reads those variables: all classes allocate from one counter and one free list (__wurst_objectMax, __wurst_objectFreeCount). The Lua state init was therefore just A_compiletime = A:create(), which took the next free id. The compile-time interpreter also handed out ids per inheritance group, as Jass does, so ids of unrelated classes would have collided in Lua's single id space anyway.

This dates from #1264, when Lua class instances became integer ids. It was not introduced by the shared allocator (#1493), which already used the one counter.

Fix

  • On the Lua target, the compile-time interpreter allocates every class from one id space (ProgramState.useOneObjectIdSpace), matching the runtime.
  • The Lua replay is one group sorted by id. Before the first object and before each gap it calls __wurst_resumeObjectIds(n), a backend intrinsic matched by node identity, like the other luaRaw* stubs. The backend prints it as __wurst_objectFreeCount = 0 followed by __wurst_objectMax = n. After the last object it continues after the highest id used at compile time. This is what the Jass replay does per allocator.
  • Jass output is unchanged.

Intentional difference

On Lua, an object made at run time comes after the highest compile-time id of any class. On Jass it comes after the highest id in its own inheritance group. testCompiletimeObjectIdMigrationSharesOneIdSpaceOnLua covers the Lua side; the existing ...MigrationStress still covers Jass's per-group allocators.

Tests

  • New testCompiletimeObjectKeepsIdAfterDroppedObject (Jass + interpreter + Lua). It also checks the emitted Lua replay: the id stored at compile time is __wurst_objectMax + 1 at the create.
  • testCompiletimeObjectIdMigration now also runs on Lua.
  • New testCompiletimeObjectIdMigrationSharesOneIdSpaceOnLua: two classes, 200 rounds with recycling, ids kept and distinct across classes, and new objects at max + 1 and max + 2.
  • All three new or Lua-enabled tests failed on Lua before the fix.
  • Ran locally on Windows: CompiletimeTests (50), LuaBackendAuditTests (242), LuaTranslationTests (144), DeterministicChecks (27) and ImTranslatorPinnedFunctionsTests (2), all green.
  • The change has no host-specific code. Linux and macOS are covered only by CI.

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.
@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-20T14:42:50.418019Z 2d92016 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 2d92016085

ℹ️ 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 62c9364 into master Oct 20, 2026
2 checks passed
Frotty added a commit that referenced this pull request Oct 21, 2026
…statics (#1528)

An object of a new-generic class made at compile time (class Box<T:> with
a field T value, kept by compiletime(build()) or a @compiletime global) was
lost on both targets. Jass stopped with "Variable Box<integer>_compiletime
not attached" in initCompiletimeState, and the Lua translation with
"initCompiletimeState_0 uses the erased generic static
Box<integer>_compiletime" in unit-test mode. A non-generic class worked.

The global holding the object was traced to the object's trace, its
allocation, which lies in class Box. EliminateGenerics.identifyGenericGlobals
takes every global traced into a generic class for a static field of that
class, so the holding global was specialised (and the original detached on
Jass) while the compile-time state still wrote the original.

The holding global is now traced to the program, as the globals for
compile-time handles are: it holds one object and is no static of the
object's class. The replay of the object's fields and allocation keeps the
object's trace.

testPersistCompiletimeNewGenericClass, which could only check the
translation because of this, now runs the program on both targets. New
tests cover the object kept by an expression and by a @compiletime global,
a class with a @compiletime static T, two instantiations, a subclass of a
generic class, an object held by an old-generic object, -inline
-localOptimizations, and on Lua the id kept after dropped objects (#1504).
@Frotty
Frotty deleted the claude/dazzling-ellis-e2cbb6 branch November 25, 2026 08:53
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