changeset 787:c49980cdece6

use incremental cloning to serve web pages
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Sep 2016 00:18:00 -0600
parents fe63c508a177
children c9409e28daa2
files src/luan/LuanState.java src/luan/impl/Closure.java src/luan/modules/IoLuan.java src/luan/modules/PackageLuan.java src/luan/modules/http/HttpServicer.java
diffstat 5 files changed, 47 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
diff -r fe63c508a177 -r c49980cdece6 src/luan/LuanState.java
--- a/src/luan/LuanState.java	Fri Sep 02 17:43:29 2016 -0600
+++ b/src/luan/LuanState.java	Sun Sep 04 00:18:00 2016 -0600
@@ -17,6 +17,7 @@
 	public LuanJava java;
 	private Map registry;
 	private final List<Reference<Closeable>> onClose = new ArrayList<Reference<Closeable>>();
+	public boolean isLocked = false;
 
 	public LuanState() {
 		java = new LuanJava();
@@ -33,16 +34,11 @@
 		LuanState clone = (LuanState)dc;
 		clone.registry = cloner.clone(registry);
 		clone.java = (LuanJava)cloner.clone(java);
-/*
-		if( !cloner.deep ) {
-			LuanCloner myCloner = new LuanCloner(false);
-			registry = myCloner.clone(registry);
-			java = (LuanJava)myCloner.clone(java);
-		}
-*/
+		if( cloner.type == LuanCloner.Type.INCREMENTAL )
+			isLocked = true;
 	}
 
-	public final Map registry() {
+	public Map registry() {
 		return registry;
 	}
 
diff -r fe63c508a177 -r c49980cdece6 src/luan/impl/Closure.java
--- a/src/luan/impl/Closure.java	Fri Sep 02 17:43:29 2016 -0600
+++ b/src/luan/impl/Closure.java	Sun Sep 04 00:18:00 2016 -0600
@@ -52,6 +52,8 @@
 	}
 
 	@Override public final Object call(LuanState luan,Object[] args) throws LuanException {
+		if( luan.isLocked )
+			throw new RuntimeException("luan is locked");
 		check();
 		LuanJava old = luan.java;
 		luan.java = ljava;
diff -r fe63c508a177 -r c49980cdece6 src/luan/modules/IoLuan.java
--- a/src/luan/modules/IoLuan.java	Fri Sep 02 17:43:29 2016 -0600
+++ b/src/luan/modules/IoLuan.java	Sun Sep 04 00:18:00 2016 -0600
@@ -685,7 +685,13 @@
 		LuanFunction opener = (LuanFunction)schemes.get(luan,scheme);
 		if( opener == null )
 			throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" );
-		return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
+		boolean isLocked = luan.isLocked;  // hack needed for luan scheme functions
+		luan.isLocked = false;
+		try {
+			return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
+		} finally {
+			luan.isLocked = isLocked;
+		}
 	}
 
 	public static final class LuanSocket extends LuanIO {
diff -r fe63c508a177 -r c49980cdece6 src/luan/modules/PackageLuan.java
--- a/src/luan/modules/PackageLuan.java	Fri Sep 02 17:43:29 2016 -0600
+++ b/src/luan/modules/PackageLuan.java	Sun Sep 04 00:18:00 2016 -0600
@@ -68,12 +68,39 @@
 		LuanTable t = IoLuan.uri(luan,uri,null);
 		if( t == null )
 			return null;
+/*
 		LuanFunction existsFn = (LuanFunction)t.get(luan,"exists");
 		boolean exists = (Boolean)Luan.first(existsFn.call(luan));
 		if( !exists )
 			return null;
 		LuanFunction reader = (LuanFunction)t.get(luan,"read_text");
 		return (String)Luan.first(reader.call(luan));
+*/
+		IoLuan.LuanIn in = (IoLuan.LuanIn)t.rawGet("java");
+		try {
+			if( !in.exists() )
+				return null;
+			return in.read_text();
+		} catch(IOException e) {
+			throw new LuanException(e);
+		}
+	}
+
+	public static boolean wouldLoad(LuanState luan,String modName) throws LuanException {
+		LuanTable loaded = loaded(luan);
+		if( loaded.rawGet(modName) != null )
+			return false;
+		if( modName.startsWith("java:") )
+			return true;
+		LuanTable t = IoLuan.uri(luan,modName,null);
+		if( t == null )
+			return false;
+		IoLuan.LuanIn in = (IoLuan.LuanIn)t.rawGet("java");
+		try {
+			return in.exists();
+		} catch(IOException e) {
+			throw new LuanException(e);
+		}
 	}
 
 }
diff -r fe63c508a177 -r c49980cdece6 src/luan/modules/http/HttpServicer.java
--- a/src/luan/modules/http/HttpServicer.java	Fri Sep 02 17:43:29 2016 -0600
+++ b/src/luan/modules/http/HttpServicer.java	Sun Sep 04 00:18:00 2016 -0600
@@ -46,6 +46,12 @@
 	{
 		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));
+				}
+			}
 			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);
@@ -65,7 +71,7 @@
 				}
 				fn = (LuanFunction)PackageLuan.require(luan,modName);
 			} else {
-				LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
+				LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
 				luan = (LuanState)cloner.clone(luan);
 				fn = (LuanFunction)cloner.get(mod);
 			}