Skip to content

Commit a4927ee

Browse files
committed
Fix read-only value error in hash assignment
The setFromList() method was directly using RuntimeScalar values from the iterator, which could be read-only. This caused 'Modification of a read-only value attempted' errors when localizing hash elements. Fixed by creating new RuntimeScalar instances, matching the behavior of createHashNoWarn() which properly handles aliasing and avoids read-only issues. Fixes failing tests: - unit/local.t - unit/lvalue_ternary.t - unit/dereference_syntax.t
1 parent 2743963 commit a4927ee

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/main/java/org/perlonjava/runtime/RuntimeHash.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,10 @@ public RuntimeArray setFromList(RuntimeList value) {
212212
// This reuses the existing StableHashMap and its capacity
213213
Iterator<RuntimeScalar> iter = value.iterator();
214214
while (iter.hasNext()) {
215-
RuntimeScalar key = iter.next();
216-
if (iter.hasNext()) {
217-
RuntimeScalar val = iter.hasNext() ? iter.next() : RuntimeScalarCache.scalarUndef;
218-
this.elements.put(key.toString(), val);
219-
}
215+
String key = iter.next().toString();
216+
// Create a new RuntimeScalar to properly handle aliasing and avoid read-only issues
217+
RuntimeScalar val = iter.hasNext() ? new RuntimeScalar(iter.next()) : new RuntimeScalar();
218+
this.elements.put(key, val);
220219
}
221220

222221
// Create a RuntimeArray that wraps this hash

0 commit comments

Comments
 (0)