changeset 626:53a50c70c5e2

add Luan.assert_function() and Http.run_later()
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 17 Jan 2016 06:19:46 -0700
parents a3c1e11fb6aa
children a98812908fbc
files core/src/luan/modules/BasicLuan.java core/src/luan/modules/Luan.luan http/src/luan/modules/http/Http.luan http/src/luan/modules/http/HttpServicer.java website/src/manual.html.luan
diffstat 5 files changed, 49 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
diff -r a3c1e11fb6aa -r 53a50c70c5e2 core/src/luan/modules/BasicLuan.java
--- a/core/src/luan/modules/BasicLuan.java	Tue Jan 12 23:52:56 2016 -0700
+++ b/core/src/luan/modules/BasicLuan.java	Sun Jan 17 06:19:46 2016 -0700
@@ -157,6 +157,11 @@
 		return v;
 	}
 
+	public static LuanFunction assert_function(LuanState luan,LuanFunction v) throws LuanException {
+		Utils.checkNotNull(luan,v);
+		return v;
+	}
+
 	public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException {
 		final double step = stepV==null ? 1.0 : stepV;
 		if( step == 0.0 )
diff -r a3c1e11fb6aa -r 53a50c70c5e2 core/src/luan/modules/Luan.luan
--- a/core/src/luan/modules/Luan.luan	Tue Jan 12 23:52:56 2016 -0700
+++ b/core/src/luan/modules/Luan.luan	Sun Jan 17 06:19:46 2016 -0700
@@ -5,6 +5,9 @@
 
 M.assert_binary = BasicLuan.assert_binary
 M.assert_boolean = BasicLuan.assert_boolean
+M.assert_function = BasicLuan.assert_function
+M.assert_integer = BasicLuan.assert_integer
+M.assert_long = BasicLuan.assert_long
 M.assert_number = BasicLuan.assert_number
 M.assert_string = BasicLuan.assert_string
 M.assert_table = BasicLuan.assert_table
diff -r a3c1e11fb6aa -r 53a50c70c5e2 http/src/luan/modules/http/Http.luan
--- a/http/src/luan/modules/http/Http.luan	Tue Jan 12 23:52:56 2016 -0700
+++ b/http/src/luan/modules/http/Http.luan	Sun Jan 17 06:19:46 2016 -0700
@@ -164,4 +164,6 @@
 	end
 end
 
+M.run_later = HttpServicer.run_later
+
 return M
diff -r a3c1e11fb6aa -r 53a50c70c5e2 http/src/luan/modules/http/HttpServicer.java
--- a/http/src/luan/modules/http/HttpServicer.java	Tue Jan 12 23:52:56 2016 -0700
+++ b/http/src/luan/modules/http/HttpServicer.java	Sun Jan 17 06:19:46 2016 -0700
@@ -11,6 +11,8 @@
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.Enumeration;
+import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.ServletException;
 import javax.servlet.http.Cookie;
@@ -157,6 +159,7 @@
 		module.rawPut("response",responseTbl);
 
 		fn.call(luan);
+		handle_run_later(luan);
 		return true;
 	}
 
@@ -256,4 +259,34 @@
 		}
 	}
 
+
+
+	private static String RUN_LATER_KEY = "Http.run_later";
+	private static final Executor exec = Executors.newSingleThreadExecutor();
+
+	public static void run_later(final LuanState luan,final LuanFunction fn,final Object... args) {
+		List list = (List)luan.registry().get(RUN_LATER_KEY);
+		if( list == null ) {
+			list = new ArrayList();
+			luan.registry().put(RUN_LATER_KEY,list);
+		}
+		list.add(
+			new Runnable(){public void run() {
+				try {
+					fn.call(luan,args);
+				} catch(LuanException e) {
+					e.printStackTrace();
+				}
+			}}
+		);
+	}
+
+	private static void handle_run_later(LuanState luan) {
+		List list = (List)luan.registry().get(RUN_LATER_KEY);
+		if( list==null )
+			return;
+		for( Object obj : list ) {
+			exec.execute((Runnable)obj);
+		}
+	}
 }
diff -r a3c1e11fb6aa -r 53a50c70c5e2 website/src/manual.html.luan
--- a/website/src/manual.html.luan	Tue Jan 12 23:52:56 2016 -0700
+++ b/website/src/manual.html.luan	Sun Jan 17 06:19:46 2016 -0700
@@ -1867,6 +1867,12 @@
 Like <a href="#Luan.assert_binary"><code>assert_binary</code></a> but for type <code>boolean</code>.
 
 
+<h4 heading><a name="Luan.assert_function"><code>Luan.assert_function (v)</code></a></h4>
+
+<p>
+Like <a href="#Luan.assert_binary"><code>assert_binary</code></a> but for type <code>function</code>.
+
+
 <h4 heading><a name="Luan.assert_integer"><code>Luan.assert_integer (v)</code></a></h4>
 
 <p>