changeset 589:97c8ae330efe

add varargs to Luan.try; add Io.output_to and Io.output_of;
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 26 Aug 2015 04:38:37 -0600
parents 5fdbefa80146
children f050c30952c0
files core/src/luan/modules/BasicLuan.java core/src/luan/modules/Io.luan website/src/manual.html.luan
diffstat 3 files changed, 31 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
diff -r 5fdbefa80146 -r 97c8ae330efe core/src/luan/modules/BasicLuan.java
--- a/core/src/luan/modules/BasicLuan.java	Tue Aug 25 10:07:37 2015 -0600
+++ b/core/src/luan/modules/BasicLuan.java	Wed Aug 26 04:38:37 2015 -0600
@@ -190,7 +190,7 @@
 		return obj instanceof LuanFunction ? (LuanFunction)obj : null;
 	}
 
-	public static Object try_(LuanState luan,LuanTable blocks) throws LuanException {
+	public static Object try_(LuanState luan,LuanTable blocks,Object... args) throws LuanException {
 		Utils.checkNotNull(luan,blocks);
 		Object obj = blocks.get(luan,1);
 		if( obj == null )
@@ -213,7 +213,7 @@
 			finallyFn = (LuanFunction)obj;
 		}
 		try {
-			return tryFn.call(luan);
+			return tryFn.call(luan,args);
 		} catch(LuanException e) {
 			if( catchFn == null )
 				throw e;
diff -r 5fdbefa80146 -r 97c8ae330efe core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Tue Aug 25 10:07:37 2015 -0600
+++ b/core/src/luan/modules/Io.luan	Wed Aug 26 04:38:37 2015 -0600
@@ -46,6 +46,30 @@
 end
 
 
+function M.output_to(out,fn,...)
+	local old_out = M.stdout
+	return try( {
+		function(...)
+			M.stdout = out
+			fn(...)
+		end;
+		finally = function()
+			M.stdout = old_out
+		end;
+	}, ... )
+end
+
+local uri = M.uri  -- make local
+
+function M.output_of(fn,...)
+	local string_uri = uri "string:"
+	local out = string_uri.text_writer()
+	M.output_to(out,fn,...)
+	out.close()
+	return string_uri.read_text()
+end
+
+
 -- repr
 
 local function do_repr(obj,done)
@@ -87,22 +111,8 @@
 	end
 end
 
-local uri = M.uri  -- make local
-
 function M.repr(obj)
-	local old_out = M.stdout
-	return try {
-		function()
-			local string_uri = uri "string:"
-			M.stdout = string_uri.text_writer()
-			do_repr(obj,{})
-			M.stdout.close()
-			return string_uri.read_text()
-		end;
-		finally = function()
-			M.stdout = old_out
-		end;
-	}
+	return M.output_of(do_repr,obj,{})
 end
 
 
diff -r 5fdbefa80146 -r 97c8ae330efe website/src/manual.html.luan
--- a/website/src/manual.html.luan	Tue Aug 25 10:07:37 2015 -0600
+++ b/website/src/manual.html.luan	Wed Aug 26 04:38:37 2015 -0600
@@ -2176,10 +2176,10 @@
 
 
 
-<h4 heading><a name="Luan.try"><code>Luan.try (t)</code></a></h4>
-
-<p>
-Implements try-catch as found in other languages where each block is in table <code>t</code>.  <code>t[1]</code> is the "try" block.  The <code>t.catch</code> and <code>t.finally</code> blocks are optional.  Returns the result of the "try" block or the "catch" block.
+<h4 heading><a name="Luan.try"><code>Luan.try (t, &middot;&middot;&middot;)</code></a></h4>
+
+<p>
+Implements try-catch as found in other languages where each block is in table <code>t</code>.  <code>t[1]</code> is the "try" block.  The <code>t.catch</code> and <code>t.finally</code> blocks are optional.  Any extra arguments are passed to the "try" function.  Returns the result of the "try" block or the "catch" block.
 
 <p>
 Example use: