changeset 126:0149bdf98fd8

add ThreadLib git-svn-id: https://luan-java.googlecode.com/svn/trunk@127 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 06 Jun 2014 14:16:38 +0000
parents 0cd559a16758
children bcb3a09d0caf
files src/luan/DeepCloner.java src/luan/LuanState.java src/luan/interp/LuanStateImpl.java src/luan/lib/ThreadLib.java src/luan/tools/WebServlet.java
diffstat 5 files changed, 60 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
diff -r 0cd559a16758 -r 0149bdf98fd8 src/luan/DeepCloner.java
--- a/src/luan/DeepCloner.java	Fri Jun 06 05:59:11 2014 +0000
+++ b/src/luan/DeepCloner.java	Fri Jun 06 14:16:38 2014 +0000
@@ -28,18 +28,18 @@
 			cloned.put(obj,rtn);
 			for( int i=0; i<rtn.length; i++ ) {
 				@SuppressWarnings("unchecked")
-				T t = (T)get(rtn[i]);
+				T t = get(rtn[i]);
 				rtn[i] = t;
 			}
 		}
 		return rtn;
 	}
 
-	public Object get(Object obj) {
+	public <T> T get(T obj) {
 		if( !(obj instanceof DeepCloneable) )
 			return obj;
 		@SuppressWarnings("unchecked")
-		DeepCloneable dc = deepClone((DeepCloneable)obj);
+		T dc = (T)deepClone((DeepCloneable)obj);
 		return dc;
 	}
 }
diff -r 0cd559a16758 -r 0149bdf98fd8 src/luan/LuanState.java
--- a/src/luan/LuanState.java	Fri Jun 06 05:59:11 2014 +0000
+++ b/src/luan/LuanState.java	Fri Jun 06 14:16:38 2014 +0000
@@ -16,6 +16,7 @@
 import luan.lib.HtmlLib;
 import luan.lib.BinaryLib;
 import luan.lib.IoLib;
+import luan.lib.ThreadLib;
 
 
 public abstract class LuanState implements DeepCloneable<LuanState> {
@@ -42,10 +43,6 @@
 		mtGetters = new ArrayList<MetatableGetter>(luan.mtGetters);
 	}
 
-	public final LuanState deepClone() {
-		return new DeepCloner().deepClone(this);
-	}
-
 	@Override public void deepenClone(LuanState clone,DeepCloner cloner) {
 		clone.global = cloner.deepClone(global);
 		clone.loaded = cloner.deepClone(loaded);
@@ -114,6 +111,7 @@
 			HtmlLib.load(luan);
 			BinaryLib.load(luan);
 			IoLib.load(luan);
+			ThreadLib.load(luan);
 			BasicLib.do_java_resource(luan,"luan/lib/init.luan");
 			JavaLib.load(luan);
 			return luan;
diff -r 0cd559a16758 -r 0149bdf98fd8 src/luan/interp/LuanStateImpl.java
--- a/src/luan/interp/LuanStateImpl.java	Fri Jun 06 05:59:11 2014 +0000
+++ b/src/luan/interp/LuanStateImpl.java	Fri Jun 06 14:16:38 2014 +0000
@@ -66,8 +66,8 @@
 	}
 
 	@Override public LuanState shallowClone() {
-		if( frame != null )
-			throw new IllegalStateException("frame isn't null");
+//		if( frame != null )
+//			throw new IllegalStateException("frame isn't null");
 		return new LuanStateImpl(this);
 	}
 
diff -r 0cd559a16758 -r 0149bdf98fd8 src/luan/lib/ThreadLib.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/lib/ThreadLib.java	Fri Jun 06 14:16:38 2014 +0000
@@ -0,0 +1,51 @@
+package luan.lib;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import luan.LuanState;
+import luan.LuanFunction;
+import luan.LuanTable;
+import luan.LuanJavaFunction;
+import luan.LuanException;
+import luan.DeepCloner;
+
+
+public final class ThreadLib {
+
+	public static void load(LuanState luan) throws LuanException {
+		luan.load("Thread",LOADER);
+	}
+
+	public static final LuanFunction LOADER = new LuanFunction() {
+		@Override public Object call(LuanState luan,Object[] args) {
+			LuanTable module = new LuanTable();
+			try {
+				add( module, "fork", LuanState.class, LuanFunction.class, new Object[0].getClass() );
+			} catch(NoSuchMethodException e) {
+				throw new RuntimeException(e);
+			}
+			return module;
+		}
+	};
+
+	private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
+		t.put( method, new LuanJavaFunction(ThreadLib.class.getMethod(method,parameterTypes),null) );
+	}
+
+	private static final Executor exec = Executors.newCachedThreadPool();
+
+	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 Object[] newArgs = cloner.deepClone(args);
+		exec.execute(new Runnable(){public void run() {
+			try {
+				newLuan.JAVA.call(newFn,"<forked>",newArgs);
+			} catch(LuanException e) {
+				e.printStackTrace();
+			}
+		}});
+	}
+
+}
diff -r 0cd559a16758 -r 0149bdf98fd8 src/luan/tools/WebServlet.java
--- a/src/luan/tools/WebServlet.java	Fri Jun 06 05:59:11 2014 +0000
+++ b/src/luan/tools/WebServlet.java	Fri Jun 06 14:16:38 2014 +0000
@@ -11,6 +11,7 @@
 import luan.LuanRuntimeException;
 import luan.LuanFunction;
 import luan.LuanElement;
+import luan.DeepCloner;
 import luan.interp.LuanCompiler;
 import luan.lib.HttpLib;
 import luan.lib.BasicLib;
@@ -46,7 +47,7 @@
 					HttpLib.load(luanState);
 				}
 			}
-			LuanState luan = luanState.deepClone();
+			LuanState luan = new DeepCloner().deepClone(luanState);
 			service(request,response,luan);
 		} catch(LuanException e) {
 			throw new LuanRuntimeException(e);