changeset 798:9eccfede1094

fix cloning bug
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 06 Sep 2016 12:23:03 -0600
parents 86e477945676
children 9c13a15a4002
files src/luan/modules/PackageLuan.java src/luan/modules/http/HttpServicer.java src/luan/modules/http/LuanHandler.java
diffstat 3 files changed, 35 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
diff -r 86e477945676 -r 9eccfede1094 src/luan/modules/PackageLuan.java
--- a/src/luan/modules/PackageLuan.java	Tue Sep 06 11:50:30 2016 -0600
+++ b/src/luan/modules/PackageLuan.java	Tue Sep 06 12:23:03 2016 -0600
@@ -8,6 +8,7 @@
 import luan.LuanTable;
 import luan.LuanFunction;
 import luan.LuanJavaFunction;
+import luan.LuanCloner;
 import luan.LuanException;
 
 
@@ -86,7 +87,7 @@
 		}
 	}
 
-	public static boolean wouldLoad(LuanState luan,String modName) throws LuanException {
+	private static boolean wouldLoad(LuanState luan,String modName) throws LuanException {
 		LuanTable loaded = loaded(luan);
 		if( loaded.rawGet(modName) != null )
 			return false;
@@ -103,4 +104,16 @@
 		}
 	}
 
+	public static void enableLoad(LuanState luan,String... mods) throws LuanException {
+		if( luan.isLocked ) {
+			for( String mod : mods ) {
+				if( wouldLoad(luan,mod) ) {
+					luan.isLocked = false;
+					luan.deepenClone(luan,new LuanCloner(LuanCloner.Type.COMPLETE));
+					return;
+				}
+			}
+		}
+	}
+
 }
diff -r 86e477945676 -r 9eccfede1094 src/luan/modules/http/HttpServicer.java
--- a/src/luan/modules/http/HttpServicer.java	Tue Sep 06 11:50:30 2016 -0600
+++ b/src/luan/modules/http/HttpServicer.java	Tue Sep 06 12:23:03 2016 -0600
@@ -45,12 +45,7 @@
 	{
 		LuanFunction fn;
 		synchronized(luan) {
-			if( luan.isLocked ) {
-				if( PackageLuan.wouldLoad(luan,"luan:http/Http.luan") || PackageLuan.wouldLoad(luan,modName) ) {
-					luan.isLocked = false;
-					luan.deepenClone(luan,new LuanCloner(LuanCloner.Type.COMPLETE));
-				}
-			}
+			PackageLuan.enableLoad(luan,"luan:http/Http.luan",modName);
 			LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
 			LuanTable per_session_pages = (LuanTable)module.rawGet("per_session_pages");
 			Object mod = PackageLuan.load(luan,modName);
diff -r 86e477945676 -r 9eccfede1094 src/luan/modules/http/LuanHandler.java
--- a/src/luan/modules/http/LuanHandler.java	Tue Sep 06 11:50:30 2016 -0600
+++ b/src/luan/modules/http/LuanHandler.java	Tue Sep 06 12:23:03 2016 -0600
@@ -101,17 +101,25 @@
 */
 
 	public Object call_rpc(String fnName,Object... args) throws LuanException {
-		LuanState luan = this.luan;
-		synchronized(luan) {
-			LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
-			luan = (LuanState)cloner.clone(luan);
+		lock.readLock().lock();
+		try {
+			LuanFunction fn;
+			LuanState luan = this.luan;
+			synchronized(luan) {
+				PackageLuan.enableLoad(luan,"luan:Rpc.luan");
+				LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan");
+				LuanTable fns = (LuanTable)rpc.get(luan,"functions");
+				fn = (LuanFunction)fns.get(luan,fnName);
+				if( fn == null )
+					throw new LuanException( "function not found: " + fnName );
+				LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
+				luan = (LuanState)cloner.clone(luan);
+				fn = (LuanFunction)cloner.get(fn);
+			}
+			return fn.call(luan,args);
+		} finally {
+			lock.readLock().unlock();
 		}
-		LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan");
-		LuanTable fns = (LuanTable)rpc.get(luan,"functions");
-		LuanFunction fn = (LuanFunction)fns.get(luan,fnName);
-		if( fn == null )
-			throw new LuanException( "function not found: " + fnName );
-		return fn.call(luan,args);
 	}
 
 	public void reset_luan() {
@@ -146,6 +154,8 @@
 	public Object runLuan(String sourceText,String sourceName) throws LuanException {
 		LuanFunction fn = Luan.load(sourceText,sourceName);
 		synchronized(luan) {
+			LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
+			LuanState luan = (LuanState)cloner.clone(this.luan);
 			return fn.call(luan);
 		}
 	}