Fixed issue: #96

This commit is contained in:
UnlegitDqrk
2026-03-02 12:52:07 +01:00
parent 85ed36de51
commit 98437da1fa
2 changed files with 23 additions and 2 deletions

View File

@@ -119,8 +119,12 @@ public class WeakTable implements Metatable {
if ( key != null && value != null ) { if ( key != null && value != null ) {
return new LuaTable.NormalEntry(key, value); return new LuaTable.NormalEntry(key, value);
} else { } else {
if ( key == null ) {
this.key = null; this.key = null;
}
if ( value == null ) {
this.value = null; this.value = null;
}
return null; return null;
} }
} }

View File

@@ -104,6 +104,23 @@ abstract public class WeakTableTest extends TableTest {
assertEquals(LuaValue.NIL, t.get(1)); assertEquals(LuaValue.NIL, t.get(1));
assertFalse("strings should not be in weak references", t.get("string").isnil()); assertFalse("strings should not be in weak references", t.get("string").isnil());
} }
public void testWeakValueRehashAfterGcDoesNotDropStrongKeyOrThrow() {
LuaTable t = new_Table();
for ( int i = 0; i < 100; i++ ) {
t.set("dead-" + i, new LuaTable());
}
collectGarbage();
for ( int i = 0; i < 100; i++ ) {
assertEquals(LuaValue.NIL, t.get("dead-" + i));
}
t.set("survivor", LuaValue.valueOf("ok"));
assertEquals(LuaValue.valueOf("ok"), t.get("survivor"));
}
} }
public static class WeakKeyTableTest extends WeakTableTest { public static class WeakKeyTableTest extends WeakTableTest {