Skip to content

Language: closure use (&$var) JIT by-ref for native lvalues (#72) - #3274

Merged
PurHur merged 1 commit into
masterfrom
agent/issue-72-closure-use-by-ref
May 30, 2026
Merged

Language: closure use (&$var) JIT by-ref for native lvalues (#72)#3274
PurHur merged 1 commit into
masterfrom
agent/issue-72-closure-use-by-ref

Conversation

@PurHur

@PurHur PurHur commented May 29, 2026

Copy link
Copy Markdown
Owner

Summary

  • Fix JIT lowering for use (&$var) when the enclosed variable is a native scalar ($x = 1): promote to a shared __value__ box at capture time and pass __value__* through ClosureWithCaptures.
  • Route assignments to aliased capture slots via JitValueBox::assignToPointer (native literals and boxed values).
  • VM parity was already green; add closure_use_byref_mutate.phpt for the issue repro ($f(); echo $x2).

php-src reference

  • Zend/zend_closures.czend_create_closure, bound variable table
  • Zend/zend_compile.czend_compile_var_dep / use (&$x)

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit test/compliance/ClosureVMTest.php --filter "closure_use_byref"'
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit test/unit/ClosureJitCompileTest.php --filter testClosureModuleVerify'
php bin/vm.php -r '$x = 1; $f = function () use (&$x) { $x = 2; }; $f(); echo $x;'
# → 2

Note: bin/jit.php MCJIT execute still segfaults in this harness (exit 139; pre-existing #98). LLVM module verify passes for all closure fixtures including closure_use_byref_mutate.phpt. AOT closure link remains out of scope (#1492).

Closes #72

Made with Cursor

Promote enclosing native scalars to boxed __value__ slots at capture time
so by-ref closure invokes pass __value__* to Native::compileArg, and assign
through valueBoxAliasPtr writes literals into the shared cell. Adds VM
compliance for mutating captures and extends LLVM module-verify fixtures.

php-src: Zend/zend_closures.c (zend_bind_var), zend_compile_var_dep
Co-authored-by: Cursor <cursoragent@cursor.com>
@PurHur
PurHur force-pushed the agent/issue-72-closure-use-by-ref branch from a75dd4d to a05d518 Compare May 30, 2026 17:24
@PurHur
PurHur merged commit 8240cfa into master May 30, 2026
@PurHur
PurHur deleted the agent/issue-72-closure-use-by-ref branch May 30, 2026 17:25
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.

Language: Closures and anonymous functions

1 participant