Mercurial Hosting > luan
changeset 72:cd9dbd7477ca
prevent stack overflow when printing table with circular references
git-svn-id: https://luan-java.googlecode.com/svn/trunk@73 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 12 Feb 2013 05:44:15 +0000 |
parents | 5a93129995e1 |
children | f86e4f77ef32 |
files | src/luan/LuanTable.java |
diffstat | 1 files changed, 19 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
diff -r 5a93129995e1 -r cd9dbd7477ca src/luan/LuanTable.java --- a/src/luan/LuanTable.java Sat Feb 02 18:14:44 2013 +0000 +++ b/src/luan/LuanTable.java Tue Feb 12 05:44:15 2013 +0000 @@ -10,6 +10,7 @@ import java.util.Comparator; import java.util.Set; import java.util.HashSet; +import java.util.IdentityHashMap; public class LuanTable { @@ -80,7 +81,14 @@ } @Override public String toString() { + return toString( Collections.newSetFromMap(new IdentityHashMap<LuanTable,Boolean>()) ); + } + + private String toString(Set<LuanTable> set) { // return "table: " + Integer.toHexString(hashCode()); + if( !set.add(this) ) { + return "..."; + } StringBuilder sb = new StringBuilder(); sb.append('{'); boolean isFirst = true; @@ -98,7 +106,7 @@ } if( gotNull ) sb.append(i+1).append('='); - sb.append(Luan.toString(obj)); + sb.append(toString(set,obj)); } } } @@ -109,13 +117,22 @@ } else { sb.append(", "); } - sb.append(Luan.toString(entry.getKey())).append('=').append(Luan.toString(entry.getValue())); + sb.append(toString(set,entry.getKey())).append('=').append(toString(set,entry.getValue())); } } sb.append('}'); return sb.toString(); } + private static String toString(Set<LuanTable> set,Object obj) { + if( obj instanceof LuanTable ) { + LuanTable t = (LuanTable)obj; + return t.toString(set); + } else { + return Luan.toString(obj); + } + } + public Object get(Object key) { if( list != null ) { Integer iT = Luan.asInteger(key);