Mercurial Hosting > luan
changeset 433:c6bcb8859b93
make LuanState.registry a Map;
remove generics from DeepCloneable;
add Map support to DeepCloner;
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 03 May 2015 15:45:39 -0600 |
parents | d9df6d6cb927 |
children | 472fc70853cd |
files | core/src/luan/DeepCloneable.java core/src/luan/DeepCloner.java core/src/luan/LuanState.java core/src/luan/LuanTable.java core/src/luan/impl/Closure.java core/src/luan/impl/UpValue.java core/src/luan/modules/IoLuan.java core/src/luan/modules/JavaLuan.java core/src/luan/modules/PackageLuan.java core/src/luan/modules/ThreadLuan.java web/src/luan/modules/web/HttpServicer.java |
diffstat | 11 files changed, 61 insertions(+), 44 deletions(-) [+] |
line wrap: on
line diff
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/DeepCloneable.java --- a/core/src/luan/DeepCloneable.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/DeepCloneable.java Sun May 03 15:45:39 2015 -0600 @@ -1,7 +1,7 @@ package luan; -public interface DeepCloneable<T extends DeepCloneable> { - public T shallowClone(); - public void deepenClone(T clone,DeepCloner cloner); +public interface DeepCloneable { + public DeepCloneable shallowClone(); + public void deepenClone(DeepCloneable clone,DeepCloner cloner); }
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/DeepCloner.java --- a/core/src/luan/DeepCloner.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/DeepCloner.java Sun May 03 15:45:39 2015 -0600 @@ -1,17 +1,17 @@ package luan; import java.util.Map; +import java.util.HashMap; import java.util.IdentityHashMap; public final class DeepCloner { - private final Map<Object,Object> cloned = new IdentityHashMap<Object,Object>(); + private final Map cloned = new IdentityHashMap(); - public <T extends DeepCloneable<T>> T deepClone(T obj) { + public DeepCloneable deepClone(DeepCloneable obj) { if( obj==null ) return null; - @SuppressWarnings("unchecked") - T rtn = (T)cloned.get(obj); + DeepCloneable rtn = (DeepCloneable)cloned.get(obj); if( rtn == null ) { rtn = obj.shallowClone(); cloned.put(obj,rtn); @@ -20,28 +20,41 @@ return rtn; } - public <T> T[] deepClone(T[] obj) { + public Object[] deepClone(Object[] obj) { if( obj.length == 0 ) return obj; - @SuppressWarnings("unchecked") - T[] rtn = (T[])cloned.get(obj); + Object[] rtn = (Object[])cloned.get(obj); if( rtn == null ) { rtn = obj.clone(); cloned.put(obj,rtn); for( int i=0; i<rtn.length; i++ ) { - @SuppressWarnings("unchecked") - T t = get(rtn[i]); - rtn[i] = t; + rtn[i] = get(rtn[i]); } } return rtn; } - public <T> T get(T obj) { - if( !(obj instanceof DeepCloneable) ) - return obj; - @SuppressWarnings("unchecked") - T dc = (T)deepClone((DeepCloneable)obj); - return dc; + public Map deepClone(Map obj) { + if( !obj.getClass().equals(HashMap.class) ) + throw new RuntimeException("can only clone HashMap"); + Map rtn = (Map)cloned.get(obj); + if( rtn == null ) { + rtn = new HashMap(); + for( Object stupid : obj.entrySet() ) { + Map.Entry entry = (Map.Entry)stupid; + rtn.put( get(entry.getKey()), get(entry.getValue()) ); + } + } + return rtn; + } + + public Object get(Object obj) { + if( obj instanceof DeepCloneable ) + return deepClone((DeepCloneable)obj); + if( obj instanceof Object[] ) + return deepClone((Object[])obj); + if( obj instanceof Map ) + return deepClone((Map)obj); + return obj; } }
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/LuanState.java --- a/core/src/luan/LuanState.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/LuanState.java Sun May 03 15:45:39 2015 -0600 @@ -2,30 +2,32 @@ import java.util.List; import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; import luan.impl.LuanCompiler; import luan.modules.BasicLuan; -public abstract class LuanState implements DeepCloneable<LuanState> { +public abstract class LuanState implements DeepCloneable { final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); - private LuanTable registry; + private Map registry; protected LuanState() { - registry = new LuanTable(); + registry = new HashMap(); } protected LuanState(LuanState luan) {} - @Override public void deepenClone(LuanState clone,DeepCloner cloner) { - clone.registry = cloner.deepClone(registry); + @Override public void deepenClone(DeepCloneable clone,DeepCloner cloner) { + ((LuanState)clone).registry = cloner.deepClone(registry); } public abstract LuanTable currentEnvironment(); public abstract LuanSource currentSource(); - public final LuanTable registry() { + public final Map registry() { return registry; }
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/LuanTable.java --- a/core/src/luan/LuanTable.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/LuanTable.java Sun May 03 15:45:39 2015 -0600 @@ -15,7 +15,7 @@ import java.util.regex.Pattern; -public final class LuanTable implements DeepCloneable<LuanTable> { +public final class LuanTable implements DeepCloneable { private Map<Object,Object> map = null; private List<Object> list = null; private LuanTable metatable = null; @@ -60,7 +60,8 @@ return new LuanTable(); } - @Override public void deepenClone(LuanTable clone,DeepCloner cloner) { + @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) { + LuanTable clone = (LuanTable)dc; if( map != null ) { clone.map = newMap(); for( Map.Entry<Object,Object> entry : map.entrySet() ) { @@ -74,7 +75,7 @@ } } if( metatable != null ) - clone.metatable = cloner.get(metatable); + clone.metatable = (LuanTable)cloner.get(metatable); clone.hasJava = hasJava; }
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/impl/Closure.java --- a/core/src/luan/impl/Closure.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/impl/Closure.java Sun May 03 15:45:39 2015 -0600 @@ -9,7 +9,7 @@ import luan.DeepCloneable; -final class Closure extends LuanFunction implements DeepCloneable<Closure> { +final class Closure extends LuanFunction implements DeepCloneable { final FnDef fnDef; private UpValue[] upValues; @@ -30,8 +30,8 @@ return new Closure(this); } - @Override public void deepenClone(Closure clone,DeepCloner cloner) { - clone.upValues = cloner.deepClone(upValues); + @Override public void deepenClone(DeepCloneable clone,DeepCloner cloner) { + ((Closure)clone).upValues = (UpValue[])cloner.deepClone(upValues); } UpValue[] upValues() {
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/impl/UpValue.java --- a/core/src/luan/impl/UpValue.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/impl/UpValue.java Sun May 03 15:45:39 2015 -0600 @@ -5,7 +5,7 @@ import luan.LuanException; -final class UpValue implements DeepCloneable<UpValue> { +final class UpValue implements DeepCloneable { private Object[] stack; private int index; private boolean isClosed = false; @@ -27,7 +27,8 @@ return new UpValue(); } - @Override public void deepenClone(UpValue clone,DeepCloner cloner) { + @Override public void deepenClone(DeepCloneable dc,DeepCloner cloner) { + UpValue clone = (UpValue)dc; clone.isClosed = isClosed; if( isClosed ) { clone.value = cloner.get(value);
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/modules/IoLuan.java --- a/core/src/luan/modules/IoLuan.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/modules/IoLuan.java Sun May 03 15:45:39 2015 -0600 @@ -679,13 +679,13 @@ private static String SECURITY_KEY = "Io.Security"; private static void check(LuanState luan,String name) throws LuanException { - Security s = (Security)luan.registry().rawGet(SECURITY_KEY); + Security s = (Security)luan.registry().get(SECURITY_KEY); if( s!=null ) s.check(luan,name); } public static void setSecurity(LuanState luan,Security s) { - luan.registry().rawPut(SECURITY_KEY,s); + luan.registry().put(SECURITY_KEY,s); } private void IoLuan() {} // never
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/modules/JavaLuan.java --- a/core/src/luan/modules/JavaLuan.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/modules/JavaLuan.java Sun May 03 15:45:39 2015 -0600 @@ -465,13 +465,13 @@ private static String SECURITY_KEY = "Java.Security"; private static void check(LuanState luan,String name) throws LuanException { - Security s = (Security)luan.registry().rawGet(SECURITY_KEY); + Security s = (Security)luan.registry().get(SECURITY_KEY); if( s!=null ) s.check(luan,name); } public static void setSecurity(LuanState luan,Security s) { - luan.registry().rawPut(SECURITY_KEY,s); + luan.registry().put(SECURITY_KEY,s); } }
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/modules/PackageLuan.java --- a/core/src/luan/modules/PackageLuan.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/modules/PackageLuan.java Sun May 03 15:45:39 2015 -0600 @@ -24,10 +24,10 @@ } public static LuanTable loaded(LuanState luan) { - LuanTable tbl = (LuanTable)luan.registry().rawGet("Package.loaded"); + LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded"); if( tbl == null ) { tbl = new LuanTable(); - luan.registry().rawPut("Package.loaded",tbl); + luan.registry().put("Package.loaded",tbl); } return tbl; }
diff -r d9df6d6cb927 -r c6bcb8859b93 core/src/luan/modules/ThreadLuan.java --- a/core/src/luan/modules/ThreadLuan.java Sat May 02 23:41:59 2015 -0600 +++ b/core/src/luan/modules/ThreadLuan.java Sun May 03 15:45:39 2015 -0600 @@ -15,8 +15,8 @@ public static void fork(LuanState luan,LuanFunction fn,Object... args) { DeepCloner cloner = new DeepCloner(); - final LuanState newLuan = cloner.deepClone(luan); - final LuanFunction newFn = cloner.get(fn); + final LuanState newLuan = (LuanState)cloner.deepClone(luan); + final LuanFunction newFn = (LuanFunction)cloner.get(fn); final Object[] newArgs = cloner.deepClone(args); exec.execute(new Runnable(){public void run() { try {
diff -r d9df6d6cb927 -r c6bcb8859b93 web/src/luan/modules/web/HttpServicer.java --- a/web/src/luan/modules/web/HttpServicer.java Sat May 02 23:41:59 2015 -0600 +++ b/web/src/luan/modules/web/HttpServicer.java Sun May 03 15:45:39 2015 -0600 @@ -58,7 +58,7 @@ luan = sessionLuan; } else { DeepCloner cloner = new DeepCloner(); - luan = cloner.deepClone(luan); + luan = (LuanState)cloner.deepClone(luan); session.putValue("luan",luan); } tbl = (LuanTable)PackageLuan.require(luan,modName); @@ -66,8 +66,8 @@ } else { fn = getService(luan,tbl); DeepCloner cloner = new DeepCloner(); - luan = cloner.deepClone(luan); - fn = cloner.get(fn); + luan = (LuanState)cloner.deepClone(luan); + fn = (LuanFunction)cloner.get(fn); } }