Reply fails under ./jcpan -t Reply because
Reply::Plugin::LexicalPersistence loads PadWalker and calls:
use PadWalker 'peek_sub', 'closed_over';PerlOnJava cannot load the CPAN XS implementation directly:
Can't load loadable object for module PadWalker: no Java XS implementation available
This is a PerlOnJava runtime capability gap, not a CPAN packaging issue.
PadWalker exposes Perl lexical pads: live my variables, closure captures,
and caller-frame lexical variables. Java reflection cannot recover this from
JVM local variables or stack frames after compilation. PerlOnJava must preserve
the relevant lexical metadata itself and expose it through a Java-backed module.
- Unblock practical CPAN users such as
Replywith a small, supportable subset first. - Preserve correct aliasing: returned PadWalker values must refer to the live lexical cells, not copies.
- Keep overhead low for programs that never load PadWalker.
- Leave a clear path toward fuller PadWalker compatibility without promising unsupported behavior in the first phase.
PadWalker 2.5 exports:
peek_my peek_our closed_over peek_sub var_name set_closed_overPhase 1 should implement only:
peek_sub($coderef)closed_over($coderef)
Phase 2 should add:
peek_my($level)peek_our($level)var_name($level_or_coderef, $var_ref)
Phase 3 should consider:
set_closed_over($coderef, \%vars)
set_closed_over is intentionally last because it mutates a closure's captured
lexical bindings and has the largest risk of breaking PerlOnJava's existing
closure lifetime and reference-count tracking.
Implement a bundled PadWalker.pm facade backed by Java methods registered via
the existing XSLoader mechanism.
For closed_over($coderef):
- Return a hash reference.
- Keys are sigil-qualified lexical names such as
'$x','@items', and'%seen'. - Values are references to the live captured lexical cells.
- Include only variables used by the subroutine but declared outside it.
For peek_sub($coderef):
- Return a hash reference.
- Include captured lexical variables that PerlOnJava can name and alias correctly.
- Do not fake entries for local pad variables whose runtime cells are not
preserved on the
RuntimeCodeobject. - Document this as an initial PerlOnJava limitation.
This subset is enough for Reply::Plugin::LexicalPersistence, which calls
peek_sub($code) and then deletes keys returned by closed_over($code).
The remaining entries become Reply's persisted lexical environment.
PerlOnJava already tracks closure capture values in multiple places:
- JVM-backed code refs use captured instance fields and
RuntimeCode.capturedScalarsfor scalar lifetime tracking. - Interpreter-backed code refs use
InterpretedCode.capturedVars. - Eval-string paths carry captured variable names and runtime values for lexical visibility.
PadWalker needs a single runtime-facing metadata structure that records:
- sigil-qualified lexical name
- declaration kind where known (
my,state,our) - captured cell value (
RuntimeScalar,RuntimeArray,RuntimeHash) - whether the cell is closed over from an outer scope
Add this metadata to RuntimeCode, for example as a list or ordered map of
PadEntry records. Populate it when closures are constructed, not by scanning
Java stack frames later.
Both code-generation paths must populate equivalent metadata:
- JVM backend: when
SubroutineParserconstructs a code object with captured parameters, pass the capture names alongside the captured runtime values. - Interpreter backend: when
InterpretedCode.withCapturedVars(...)is used, retain the parallel captured variable names already computed by the bytecode compiler/eval-string path.
The ordering must be stable and should follow the existing capture ordering used for constructor/register setup. This avoids a separate name/value matching algorithm.
Add:
src/main/perl/lib/PadWalker.pmsrc/main/java/org/perlonjava/runtime/perlmodule/PadWalker.java
PadWalker.pm should match the CPAN module's public import shape but should
state in comments that this is a PerlOnJava-backed compatibility module.
PadWalker.java should register methods in package PadWalker, following
the existing Java-backed module pattern.
peek_my($level) and peek_our($level) cannot be implemented from code-ref
capture metadata alone. They require the current call stack's live lexical
environment at the call point.
Add a frame-level lexical registry only when needed:
- Introduce a lightweight
LexicalFrameruntime object containing named live lexical cells for the current scope. - Push/pop lexical frames around subroutine calls only when PadWalker support, debugger support, or an equivalent introspection mode is active.
- Expose frame lookup by PadWalker level using the same call-stack semantics
as
caller. - Keep
peek_myandpeek_ourseparate soouraliases can be filtered from truemylexicals once declaration-kind metadata is available.
Avoid always-on frame tracking until measured. Many PerlOnJava workloads depend on low overhead in normal execution, and PadWalker is mostly a debugging and REPL-support feature.
set_closed_over($coderef, \%vars) should only be implemented after Phase 1
tests prove that captured cells are represented consistently across both
backends.
Required semantics:
- Rebind only variables already closed over by the target code ref.
- Require hash values to be references, matching PadWalker behavior.
- Preserve cell type compatibility by sigil.
- Update lifetime tracking so replaced scalar captures do not leak or lose DESTROY/weaken behavior.
If these invariants cannot be met cleanly, leave set_closed_over unsupported
with a clear error.
Add focused unit tests before validating CPAN modules:
closed_over(sub { $x })returns'$x'.- Mutating
${ closed_over($sub)->{'$x'} }updates the original lexical. - Captured scalar, array, and hash variables preserve sigils and aliasing.
- Duplicate lexical names prefer the innermost captured binding.
peek_subincludes the captured variables needed by Reply.- Unsupported local pad entries are absent rather than copied or faked.
- JVM backend and interpreter backend return the same metadata.
- Closure lifetime tests still pass when PadWalker metadata is present.
Then validate:
timeout 900 ./jcpan -t Reply > /tmp/jcpan-reply-padwalker.log 2>&1
printf 'EXIT: %s\n' $? >> /tmp/jcpan-reply-padwalker.log
make > /tmp/make-padwalker.log 2>&1
printf 'EXIT: %s\n' $? >> /tmp/make-padwalker.logExpected Phase 1 result: Reply::Plugin::LexicalPersistence loads past the
PadWalker failure. Any later Reply failure should be investigated separately.
- Over-capturing every visible lexical would regress memory use and undo recent selective-capture optimizations.
- Returning copied values would make Reply appear to work while breaking lexical persistence semantics.
- Caller-frame APIs may impose runtime overhead if frame tracking is always on.
set_closed_overcan corrupt closure lifetime tracking if implemented before capture metadata is unified.
- Phase 1: code-reference lexical metadata and mutation (2026-08-07)
- Added live captured-variable maps for JVM and interpreter closures.
- Added declared local-pad names for named and anonymous subroutines.
- Implemented bundled
PadWalker::peek_sub,closed_over, andset_closed_overthroughInternals. - Added bundled
Devel::CallerandDevel::LexAliascompatibility forcaller_cvand lexical rebinding. - Made active lexical-frame tracking opt-in when Devel::LexAlias loads.
- Validated the focused unit suite with system Perl and both PerlOnJava
backends;
Devel::LexAliaspasses 11 upstream tests andLexical::Persistencepasses 54 tests. - Files include
RuntimeCode.java,Internals.java,VariableCollectorVisitor.java,EmitVariable.java,BytecodeCompiler.java,PadWalker.pm,Devel/Caller.pm, andDevel/LexAlias.pm.
- Phase 2: caller-frame pad introspection (2026-08-16)
- Added opt-in live lexical-pad registration to compiled and interpreted subroutine frames.
- Implemented
peek_myandpeek_ourwith PadWalker-compatible logical frame numbering and live scalar, array, and hash aliases. - Preserved lexical declaration and
ourmetadata through graph cloning so the two APIs can filter the active pad correctly. - Added focused system-Perl and PerlOnJava coverage as part of
cpan_tooling_runtime.t. - Files include
RuntimeCode.java,RuntimeGraphCloner.java,VariableCollectorVisitor.java,InterpretedCode.java,BytecodeCompiler.java,SubroutineParser.java,Variable.java,Internals.java, andPadWalker.pm.
- Validate
./jcpan -t Replyand diagnose any failure beyond its former PadWalker load blocker. - Add duplicate-name and closure-lifetime coverage beyond the Lexical::Persistence integration suite.
- Measure the opt-in caller-frame registry under debugger and REPL workloads.
- Should
peek_subeventually preserve local pad variables, or should compiler-maintained declaration metadata remain the supported boundary? - Should the completed Phase 2 frame registry eventually be shared with the debugger's call-stack infrastructure, or remain an independent opt-in path?