X Tutup
Skip to content

Add LuaJIT-specific code generation optimizations#12754

Open
jdonaldson wants to merge 3 commits intoHaxeFoundation:developmentfrom
jdonaldson:luajit-codegen
Open

Add LuaJIT-specific code generation optimizations#12754
jdonaldson wants to merge 3 commits intoHaxeFoundation:developmentfrom
jdonaldson:luajit-codegen

Conversation

@jdonaldson
Copy link
Member

Summary

  • When -D lua_jit is set, use goto-based continue emulation (do...end + ::_hx_continue_N:: labels) instead of repeat-until + flag variable pattern
  • Inside try-catch blocks, use distinct pcall sentinels (_hx_pcall_continue vs _hx_pcall_break) so the error handler can emit goto for continue and break for break; nested try-catch correctly re-throws sentinels
  • Add _hx_tab_array_jit.lua which uses LuaJIT's table.new for pre-allocated array construction
  • Update -D lua_jit doc string (was incorrectly referencing "version 5.2 only")

Test plan

  • All existing Lua unit tests pass with LuaJIT (luajit bin/unit.lua — 11741 assertions)
  • All existing Lua unit tests pass with vanilla Lua 5.4 (lua bin/unit.lua — 11715 assertions)
  • New tests in IssueLuaJit.hx cover: basic continue, break+continue, nested loop continue, continue in try-catch, break in try-catch, nested try-catch with continue, combined break+continue in try-catch
  • No repeat-until patterns remain in LuaJIT output (verified via grep)
  • _hx_pcall_continue sentinel appears in generated output for try-catch+continue loops

@jdonaldson
Copy link
Member Author

I found some old half-baked LuaJit optimization branch and had Claude finish it off. The optimizations are accessible with the -D luajit flag, making it actually useful for something now.


class IssueLuaJit extends Test {
function testGotoContinue() {
#if lua_jit
Copy link
Member

Choose a reason for hiding this comment

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

!

Copy link
Member Author

Choose a reason for hiding this comment

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

My bad. I gave Claude an instruction not to do this anymore, but it ignored me, and I pushed this late last night and didn't check things over properly.

So, this was helpful for me. When someone asks me to change behavior permanently with my AI tooling, I'm going to try and figure out how to do it with "rules" rather than "suggestions".

For this particular case, I added a pre-commit hook that specifically looks for this scenario and blocks pushes if it detects test gating : https://github.com/jdonaldson/dotfiles/blob/jdonaldson/.config/git/hooks/pre-commit

Thanks for helping me figure out the "new normal", sorry for being annoying, but honestly I missed you getting annoyed at my work :)

Copy link
Member

Choose a reason for hiding this comment

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

I'm not annoyed, it's ultimately a very minor thing and watching you trying to get Claude under control is quite entertaining! Also, that's a creative solution for that particular problem.

Copy link
Member Author

@jdonaldson jdonaldson Mar 6, 2026

Choose a reason for hiding this comment

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

I really should've saved the conversation, it looked at genlua and noted how I was getting around Lua's limited conditional control of flow options, gave me a nice pat on the head for it, and then one upped me here.

@tobil4sk
Copy link
Member

tobil4sk commented Mar 6, 2026

I'd imagine that some of the cases in IssueLuaJit overlap with existing haxe tests in which case there is not much point in having them. Perhaps if some are missing they can be added to the general haxe tests instead, then it would be enough to know that existing haxe unit tests pass with -D lua-jit.

Also, it looks like luajit 2.0 does not have table.new.

I was also wondering if we could also optimise bitwise operations for luajit using bit.tobit, but maybe best to merge the other bit related PRs first.

When -D lua_jit is set, use goto-based continue emulation instead of the
repeat-until + flag variable pattern. Inside try-catch blocks, use distinct
pcall sentinels (_hx_pcall_continue vs _hx_pcall_break) so the error
handler can emit goto for continue and break for break. Nested try-catch
correctly re-throws sentinels.

Also adds _hx_tab_array_jit.lua which uses LuaJIT's table.new for
pre-allocated array construction, and updates the -D lua_jit doc string.
Tests are pure Haxe with no target-specific imports, so they
should run on all targets.
- Use pcall to optionally load table.new for LuaJIT 2.0 compatibility
- Remove redundant tests (continue, break, arrays, nested loops) that
  overlap with existing test suite coverage
- Keep unique try-catch + continue/break tests that validate pcall
  sentinel codegen
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.

3 participants

X Tutup