Skip to content

Lua: skip the nil test of a native's enum argument which is a common.j constant - #1541

Merged
Frotty merged 3 commits into
masterfrom
lua-constant-enum-nil-guard
Oct 22, 2026
Merged

Frotty merged 3 commits into
masterfrom
lua-constant-enum-nil-guard

Conversation

@Frotty

@Frotty Frotty commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

On Lua every native with a handle parameter is called through a nil-safety wrapper. For an enum parameter the wrapper turns null into the handle of id 0, as the engine reads it:

t = UNIT_STATE_MAX_LIFE
if (t == nil) then t = ConvertUnitState(0) end
GetUnitState(u, t)

The wrapper was built once per native, so it tested every argument, including common.j constants such as UNIT_STATE_MAX_LIFE, which the game defines before any script runs and nothing can assign.

Change (LuaNativeLowering)

  • The tests are chosen where the native is called. Each wrapper is built for a native and a set of tested parameters, so __wurst_safe_whichUnit_GetUnitState tests only the unit; the tested parameters are named before the native, so the call still reads as a call of the native.
  • An argument that reads a common.j constant made by its converter is not tested. Such a constant is a constant BJ global whose initialiser is ConvertX(<int>).
  • A native whose every guarded argument is such a constant is called directly.
  • A local variable and a global of the program's own keep the test.
  • Jass output is unchanged.

Measurement (a large real map)

Built with -lua -inline -localOptimizations -runcompiletimefunctions -injectobjects, master ee2c6b66b against this branch. Opcodes are counted with the bundled luac53 -l -p.

enum nil tests (if x == nil then x = ConvertX(0) end) master this PR
all 494 189
given a common.j constant 471 166

Removed (305), counted by the function the test sat in:

  • 293 in setup code: InitCustomTeams 136, config 123, SetPlayerAllianceStateBJ 26, other init 8.
  • 12 on other paths (damage handlers, unit-index and player callbacks). This is a net count: inlining moved 3 tests of a non-constant attack type from a wrapper into its one caller.

Left (166 given a constant), including every gameplay one: IsUnitType 46, BlzFrameSetTextAlignment 34, GetUnitState 29, GetPlayerState 13, TriggerRegisterPlayerEvent 13, SetUnitState 12. The constant reaches these natives through a standard library parameter that only forwards it (unit.getState(unitstate state) calls GetUnitState(this, state)). The constant appears only after the inliner has removed that function, which happens after the lowering has chosen the test. The two candidate fixes are written up in BACKLOG.md ("Lua enum nil tests on gameplay paths…") for a decision; this PR leaves them out.

Static Lua instructions: 269,561 → 267,667 (−1,894, −0.7 %).

opcode master this PR Δ
MOVE 25,821 25,448 −373
CALL 17,671 17,361 −310
LOADK 35,582 35,273 −309
EQ 9,845 9,544 −301
GETTABUP 59,050 58,757 −293
JMP 26,521 26,231 −290

All the other opcodes moved by 8 or fewer. Script size went from 4,840,803 to 4,815,291 bytes. These are static counts, mostly in code that runs once. The test runtime's Lua is not the game's Lua, and nothing here was timed in game.

Tests

LuaBackendAuditTests:

  • aCommonJConstantArgumentIsNotTestedForNil: shape test. A constant gets the unit-only wrapper. A local and a global of the program's own keep the full guard. A native given only constants is called directly with no wrapper. It fails on master.
  • anInlinedCommonJConstantArgumentIsNotTestedForNil: shape test under -inline -localOptimizations.
  • aCommonJConstantArgumentReadsTheSameAsAGuardedOne: runs on Jass, the interpreter and Lua (testLua(true).luaOnly(false)). It covers a constant, a global of the program's own, a parameter, null, a null local and a null player.

Run locally on Windows: LuaBackendAuditTests, LuaTranslationTests, InterpreterTests, DeterministicChecks. The change makes no host assumptions; Linux and macOS are covered only by CI.

…j constant

The nil-safety wrapper of a native was built once per native, so it tested
every argument, including common.j constants such as UNIT_STATE_MAX_LIFE,
which are never nil. The guards are now chosen per call: a constant argument
is not tested, and a native given only constants is called directly.

An enum parameter which a function only hands on to natives' enum parameters
(unit.getState, unit.isType in the standard library) is tested by the
function's callers instead, where a constant needs no test; only when every
call of the function is seen and nothing else reads the parameter.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-22T10:08:30.240845Z 4744a16 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.

@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: 6a75186577

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

Testing a forwarded enum parameter at the callers is an interprocedural
rewrite which wants a decision of its own, so this change keeps only the
choice per call. BACKLOG.md records the gameplay tests it leaves and the two
candidate roots.

A wrapper which tests only some parameters names them before the native
(__wurst_safe_whichPlayer_SetPlayerState), so its call still reads as a call
of the native: WurstBuildMapOutputTests looks for SetPlayerState(Player(0), ...)
in the generated config script.
@Frotty

Frotty commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 6974dd2f95

ℹ️ 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 commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 4744a16bcb

ℹ️ 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 9647841 into master Oct 22, 2026
2 checks passed
@Frotty
Frotty deleted the lua-constant-enum-nil-guard 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