Skip to content

Commit

Permalink
detect cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Mar 8, 2024
1 parent 2bd1fc2 commit 47311b0
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class PerGenerator {
static final byte[] HEADER = new byte[] {0x0a, 0x0d, 0x03, 0x0f};
private final OutputStream main;
private final Map<Object, Integer> knownObjects = new IdentityHashMap<>();
private final Map<Object, Integer> enteredObjects = new IdentityHashMap<>();
private final Histogram histogram;
private final PerMap map;
final Function<Object, Object> writeReplace;
Expand Down Expand Up @@ -59,6 +60,9 @@ final <T> int writeObject(T t) throws IOException {
java.lang.Object obj = writeReplace.apply(t);
java.lang.Integer found = knownObjects.get(obj);
if (found == null) {
if (enteredObjects.put(obj, 0) != null) {
throw new IllegalStateException("Cyclic reference detected");
}
org.enso.persist.Persistance<?> p = map.forType(obj.getClass());
java.io.ByteArrayOutputStream os = new ByteArrayOutputStream();
p.writeInline(obj, new ReferenceOutput(this, os));
Expand All @@ -83,6 +87,10 @@ final void writeIndirect(Object obj, Persistance.Output out) throws IOException
}
java.lang.Integer found = knownObjects.get(obj);
if (found == null) {
if (enteredObjects.put(obj, 0) != null) {
throw new IllegalStateException("Cyclic reference detected");
}

var os = new ByteArrayOutputStream();
var osData = new ReferenceOutput(this, os);
p.writeInline(obj, osData);
Expand Down

0 comments on commit 47311b0

Please sign in to comment.