|
| 1 | +# Fix Read-Only Undef in Chop Operations |
| 2 | + |
| 3 | +## Objective |
| 4 | +Fix the "Modification of a read-only value attempted" error when using chop on `+()` (unary plus on empty list), which currently crashes op/chop.t at line 245. |
| 5 | + |
| 6 | +## Problem Statement |
| 7 | +When `chop(+())` is executed, PerlOnJava throws a read-only modification error, but standard Perl allows this operation. This causes op/chop.t to crash at test 138. |
| 8 | + |
| 9 | +## Current Status |
| 10 | +- **Test file:** t/op/chop.t |
| 11 | +- **Crash at:** line 245 (`map chop(+()), ('')x68;`) |
| 12 | +- **Error:** "Modification of a read-only value attempted" |
| 13 | +- **Impact:** Test suite crashes, unable to run remaining tests |
| 14 | + |
| 15 | +## Root Cause Analysis |
| 16 | + |
| 17 | +### Investigation Done (45 minutes) |
| 18 | +1. Used `--disassemble` to trace bytecode generation |
| 19 | +2. Found that `+()` generates: `INVOKESTATIC org/perlonjava/operators/Operator.undef()` |
| 20 | +3. `Operator.undef()` returns `scalarUndef`, a cached read-only value |
| 21 | +4. Also found `RuntimeList.scalar()` returns `scalarUndef` for empty lists |
| 22 | + |
| 23 | +### Current Implementation |
| 24 | +```java |
| 25 | +// In Operator.java |
| 26 | +public static RuntimeScalar undef() { |
| 27 | + return scalarUndef; // Returns cached read-only value |
| 28 | +} |
| 29 | + |
| 30 | +// In RuntimeList.java |
| 31 | +public RuntimeScalar scalar() { |
| 32 | + if (isEmpty()) { |
| 33 | + return scalarUndef; // Returns cached read-only value |
| 34 | + } |
| 35 | + return elements.getLast().scalar(); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Attempted Fixes That Failed |
| 40 | +1. **Changed Operator.undef() to return new RuntimeScalar()** - Still fails |
| 41 | +2. **Changed RuntimeList.scalar() to return new RuntimeScalar()** - Still fails |
| 42 | +3. **Created undefLvalue() method** - Couldn't identify where to use it |
| 43 | + |
| 44 | +### The Real Issue |
| 45 | +The problem appears to be in how `addToScalar()` works. When `+()` is evaluated: |
| 46 | +1. It calls `Operator.undef()` to get an initial value |
| 47 | +2. Then calls `addToScalar()` on that value |
| 48 | +3. But somewhere in this chain, a read-only value is being created or preserved |
| 49 | + |
| 50 | +## Proposed Solutions |
| 51 | + |
| 52 | +### Option 1: Track Lvalue Context |
| 53 | +Modify the code generation to detect when undef is needed in an lvalue context (like as an argument to chop) and use a different method. |
| 54 | + |
| 55 | +### Option 2: Make All Undefs Modifiable |
| 56 | +Always return modifiable undefs, accepting the performance cost. This is simpler but may have memory implications. |
| 57 | + |
| 58 | +### Option 3: Fix addToScalar Chain |
| 59 | +Investigate the full chain of `addToScalar()` calls to find where the read-only value is introduced. |
| 60 | + |
| 61 | +## Test Cases to Verify |
| 62 | + |
| 63 | +```perl |
| 64 | +# Minimal test case |
| 65 | +chop(+()); # Should not error |
| 66 | + |
| 67 | +# From op/chop.t line 245 |
| 68 | +map chop(+()), ('')x68; # Should not error |
| 69 | + |
| 70 | +# Comparison |
| 71 | +chop(undef); # Should error (literal undef is read-only) |
| 72 | +my $x; chop($x); # Should work (variable undef is modifiable) |
| 73 | +``` |
| 74 | + |
| 75 | +## Implementation Checklist |
| 76 | +- [ ] Identify exact code path from `+()` to chop |
| 77 | +- [ ] Determine where read-only flag is set |
| 78 | +- [ ] Implement fix to return modifiable undef in this context |
| 79 | +- [ ] Test with op/chop.t |
| 80 | +- [ ] Verify no regressions in other tests |
| 81 | + |
| 82 | +## Expected Impact |
| 83 | +- **Direct fix:** 1 crash in op/chop.t |
| 84 | +- **Indirect benefit:** Tests 138-148 can run (currently blocked by crash) |
| 85 | +- **Potential issues:** May affect performance if all undefs become modifiable |
| 86 | + |
| 87 | +## Complexity Assessment |
| 88 | +- **Estimated effort:** 1-2 hours (complex interaction between multiple systems) |
| 89 | +- **Risk level:** Medium (affects core scalar operations) |
| 90 | +- **Files involved:** |
| 91 | + - Operator.java |
| 92 | + - RuntimeList.java |
| 93 | + - RuntimeScalar.java |
| 94 | + - Possibly EmitOperator.java |
| 95 | + |
| 96 | +## Next Steps |
| 97 | +1. Trace the exact execution path with debugger |
| 98 | +2. Identify where RuntimeScalarReadOnly is introduced |
| 99 | +3. Implement targeted fix for this specific case |
| 100 | +4. Consider broader implications for other similar operations |
| 101 | + |
| 102 | +## Test Files Preserved |
| 103 | +- `dev/sandbox/readonly_undef_chop_test.pl` - Test cases for the issue |
| 104 | + |
| 105 | +## Cleanup Checklist |
| 106 | +- [ ] Remove `dev/sandbox/readonly_undef_*.pl` test files after fix |
| 107 | +- [ ] Revert changes to `Operator.java` and `RuntimeList.java` if not working |
| 108 | +- [ ] Clean up any `test_*.pl` files in project root |
| 109 | +- [ ] Move useful test cases to `src/test/resources/` if fix is successful |
| 110 | + |
| 111 | +## Code References |
| 112 | +- Bytecode generation: `EmitOperator.emitUndef()` |
| 113 | +- Operator implementation: `Operator.undef()` |
| 114 | +- List scalar conversion: `RuntimeList.scalar()` |
| 115 | +- Scalar assignment: `RuntimeList.addToScalar()` |
0 commit comments