Skip to content

Fix method receiver preservation during __index lookup - #347

Merged
akeit0 merged 1 commit into
mainfrom
codex/issue344-self-receiver
Oct 10, 2026
Merged

akeit0 merged 1 commit into
mainfrom
codex/issue344-self-receiver

Conversation

@akeit0

@akeit0 akeit0 commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

Calling obj:method(...) through a Lua __index function could pass a stale stack value as self, including a Vector3 left by a previous property setter. SELF saved the receiver only on the synchronous return path; Lua metamethod execution could reuse its slot first. The async/hook completion path also reread register B, which lookup itself can change.

Save and protect the original receiver before method lookup, matching Lua 5.2's SELF ordering. Completion writes only the method result. Reacquire the destination from the current stack buffer after synchronous C# lookup, since that callback can resize the buffer.

Fixes #344.

Validation:

  • The initial 15 regression cases had eight failures on main, including the reported property-write/LookAt structure. The final 16 cases pass and cover tail/non-tail lookup, chained table/function __index, hooks, yield, receiver-register mutation, synchronous/asynchronous C# lookup and stack growth.
  • All 307 runtime tests pass in Release, excluding ExpectedFailure. Release builds pass for net10.0/net8.0/net6.0/netstandard2.1; formatting checks pass.
  • BenchmarkDotNet 0.14.0 on .NET 8 x64 Windows; two launches, five warmups, twelve 500 ms iterations. Compilation and warmup happen in setup; each measured script performs 100k method calls and validates its numeric result.
Lookup main 991c4fc fixed Change
Direct table method 5.156 ms 4.996 ms 3.1% faster
C# __index 9.094 ms 8.832 ms 2.9% faster
Lua __index 9.170 ms 9.495 ms 3.5% slower

The Lua __index timing harness deliberately ignores self so both versions complete; main is semantically incorrect on that path, and the regression tests separately assert receiver identity. This is a correctness fix with a measured cost on Lua function lookup, rather than a claim of universal performance improvement.

@akeit0
akeit0 merged commit 7936b0d into main Oct 10, 2026
2 checks passed
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.

Method call via ":" receives the wrong "self" (Bug)

1 participant