changeset 1283:503bde9a7c80

add luan.require() and table.call()
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 21 Dec 2018 09:12:09 -0700
parents 2531942abaf3
children 1b46c8e6c647
files src/luan/LuanException.java src/luan/LuanState.java src/luan/LuanTable.java src/luan/modules/Boot.luan src/luan/modules/PackageLuan.java src/luan/modules/http/HttpServicer.java src/luan/modules/http/LuanHandler.java
diffstat 7 files changed, 43 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
diff -r 2531942abaf3 -r 503bde9a7c80 src/luan/LuanException.java
--- a/src/luan/LuanException.java	Thu Dec 20 17:44:34 2018 -0700
+++ b/src/luan/LuanException.java	Fri Dec 21 09:12:09 2018 -0700
@@ -33,27 +33,10 @@
 
 	public LuanTable table(LuanState luan) {
 		if( table==null ) {
-			table = new LuanTable(luan);
-			table.rawPut( "java", this );
-			LuanTable metatable = new LuanTable(luan);
-			table.setMetatable(metatable);
 			try {
-				table.rawPut( "get_message", new LuanJavaFunction(
-					LuanException.class.getMethod( "getMessage" ), this
-				) );
-				table.rawPut( "throw", new LuanJavaFunction(
-					LuanException.class.getMethod( "throwThis" ), this
-				) );
-				table.rawPut( "get_stack_trace_string", new LuanJavaFunction(
-					LuanException.class.getMethod( "getLuanStackTraceString" ), this
-				) );
-				table.rawPut( "get_java_stack_trace_string", new LuanJavaFunction(
-					LuanException.class.getMethod( "getJavaStackTraceString" ), this
-				) );
-				metatable.rawPut( "__to_string", new LuanJavaFunction(
-					LuanException.class.getMethod( "__to_string", LuanTable.class ), this
-				) );
-			} catch(NoSuchMethodException e) {
+				LuanTable Boot = (LuanTable)luan.require("luan:Boot.luan");
+				table = (LuanTable)Boot.call( "new_error_table", this );
+			} catch(LuanException e) {
 				throw new RuntimeException(e);
 			}
 		}
@@ -64,10 +47,6 @@
 		throw this;
 	}
 
-	public String __to_string(LuanTable ignore) {
-		return getLuanStackTraceString();
-	}
-
 	public String getJavaStackTraceString() {
 		return getJavaStackTraceString(this);
 	}
diff -r 2531942abaf3 -r 503bde9a7c80 src/luan/LuanState.java
--- a/src/luan/LuanState.java	Thu Dec 20 17:44:34 2018 -0700
+++ b/src/luan/LuanState.java	Fri Dec 21 09:12:09 2018 -0700
@@ -14,6 +14,7 @@
 import luan.impl.LuanCompiler;
 import luan.modules.BasicLuan;
 import luan.modules.JavaLuan;
+import luan.modules.PackageLuan;
 
 
 public final class LuanState implements LuanCloneable {
@@ -56,15 +57,13 @@
 			onClose.onClose(c);
 	}
 
-	public final Object eval(String cmd,Object... args) throws LuanException {
+	public Object eval(String cmd,Object... args) throws LuanException {
 		return Luan.load(cmd,"eval").call(this,args);
 	}
-/*
-	public final Object eval(String cmd,LuanTable env) throws LuanException {
-		LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true);
-		return fn.call(this);
+
+	public Object require(String modName) throws LuanException {
+		return PackageLuan.require(this,modName);
 	}
-*/
 
 	public String toString(Object obj) throws LuanException {
 		if( obj instanceof LuanTable ) {
diff -r 2531942abaf3 -r 503bde9a7c80 src/luan/LuanTable.java
--- a/src/luan/LuanTable.java	Thu Dec 20 17:44:34 2018 -0700
+++ b/src/luan/LuanTable.java	Fri Dec 21 09:12:09 2018 -0700
@@ -512,4 +512,8 @@
 		super.finalize();
 	}
 
+	public Object call(String fnName,Object... args) throws LuanException {
+		LuanFunction fn = Luan.checkFunction(get(fnName));
+		return fn.call(luan,args);
+	}
 }
diff -r 2531942abaf3 -r 503bde9a7c80 src/luan/modules/Boot.luan
--- a/src/luan/modules/Boot.luan	Thu Dec 20 17:44:34 2018 -0700
+++ b/src/luan/modules/Boot.luan	Fri Dec 21 09:12:09 2018 -0700
@@ -4,6 +4,7 @@
 local BasicLuan = require "java:luan.modules.BasicLuan"
 local new_error = BasicLuan.new_error
 local ipairs = BasicLuan.ipairs
+local set_metatable = BasicLuan.set_metatable
 local StringLuan = require "java:luan.modules.StringLuan"
 local match = StringLuan.match  -- String.match
 local IoLuan = require "java:luan.modules.IoLuan"
@@ -185,4 +186,22 @@
 end
 
 
+local error_mt = {}
+
+function error_mt.__to_string(t)
+	return t.java.getLuanStackTraceString()
+end
+
+function Boot.new_error_table(ex)  -- for LuanException.java
+	local this = {}
+	set_metatable(this,error_mt)
+	this.java = ex
+	this.get_message = ex.getMessage
+	this.throw = ex.throwThis
+	this.get_stack_trace_string = ex.getLuanStackTraceString
+	this.get_java_stack_trace_string = ex.getLuanStackTraceString
+	return this
+end
+
+
 return Boot
diff -r 2531942abaf3 -r 503bde9a7c80 src/luan/modules/PackageLuan.java
--- a/src/luan/modules/PackageLuan.java	Thu Dec 20 17:44:34 2018 -0700
+++ b/src/luan/modules/PackageLuan.java	Fri Dec 21 09:12:09 2018 -0700
@@ -87,9 +87,8 @@
 	}
 
 	static String read(LuanState luan,String uri) throws LuanException {
-		LuanTable boot = (LuanTable)PackageLuan.require(luan,"luan:Boot.luan");
-		LuanFunction read = (LuanFunction)boot.get("read");
-		return (String)Luan.first(read.call(luan,new Object[]{uri}));
+		LuanTable boot = (LuanTable)luan.require("luan:Boot.luan");
+		return (String)Luan.first(boot.call("read",uri));
 	}
 
 	public static void enableLoad(LuanState luan,String... mods) throws LuanException {
diff -r 2531942abaf3 -r 503bde9a7c80 src/luan/modules/http/HttpServicer.java
--- a/src/luan/modules/http/HttpServicer.java	Thu Dec 20 17:44:34 2018 -0700
+++ b/src/luan/modules/http/HttpServicer.java	Fri Dec 21 09:12:09 2018 -0700
@@ -33,9 +33,8 @@
 			LuanCloner cloner = new LuanCloner(LuanCloner.Type.INCREMENTAL);
 			luan = (LuanState)cloner.clone(luan);
 		}
-		LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
-		LuanFunction fn = (LuanFunction)module.rawGet("handle_error");
-		return (Response)fn.call( luan, new Object[]{request,e.table(luan)} );
+		LuanTable module = (LuanTable)luan.require("luan:http/Http.luan");
+		return (Response)module.call( "handle_error", request, e.table(luan) );
 	}
 
 	private static Response serviceLuan(LuanState luan,Request request,String modName)
@@ -55,21 +54,13 @@
 			fn = (LuanFunction)cloner.get(mod);
 		}
 
-		LuanTable module = (LuanTable)PackageLuan.require(luan,"luan:http/Http.luan");
-
-		// request
-		LuanFunction newRequestFn = (LuanFunction)module.rawGet("new_request");
-		newRequestFn.call( luan, new Object[]{request} );
-
-		// response
-		LuanFunction newResponseFn = (LuanFunction)module.rawGet("new_response");
-		newResponseFn.call(luan);
+		LuanTable module = (LuanTable)luan.require("luan:http/Http.luan");
+		module.call( "new_request", request );
+		module.call( "new_response" );
 
 		fn.call(luan);
 
-		LuanFunction finishFn = (LuanFunction)module.rawGet("finish");
-		Response response = (Response)finishFn.call(luan);
-		return response;
+		return (Response)module.call( "finish" );
 	}
 
 	private HttpServicer() {}  // never
diff -r 2531942abaf3 -r 503bde9a7c80 src/luan/modules/http/LuanHandler.java
--- a/src/luan/modules/http/LuanHandler.java	Thu Dec 20 17:44:34 2018 -0700
+++ b/src/luan/modules/http/LuanHandler.java	Fri Dec 21 09:12:09 2018 -0700
@@ -55,9 +55,9 @@
 			loggerRoot = "";
 		logger = LoggerFactory.getLogger(loggerRoot+LuanHandler.class.getName());
 		try {
-			LuanTable Http = (LuanTable)PackageLuan.require(luanInit,"luan:http/Http.luan");
-			Http.rawPut( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
-			Http.rawPut( "eval_in_root", new LuanJavaFunction(evalInRootMethod,this) );
+			LuanTable Http = (LuanTable)luanInit.require("luan:http/Http.luan");
+			Http.put( "reset_luan", new LuanJavaFunction(resetLuanMethod,this) );
+			Http.put( "eval_in_root", new LuanJavaFunction(evalInRootMethod,this) );
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
 		}
@@ -136,7 +136,7 @@
 			LuanState luan = currentLuan;
 			synchronized(luan) {
 				PackageLuan.enableLoad(luan,"luan:Rpc.luan");
-				LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan");
+				LuanTable rpc = (LuanTable)luan.require("luan:Rpc.luan");
 				LuanTable fns = (LuanTable)rpc.get("functions");
 				fn = (LuanFunction)fns.get(fnName);
 				if( fn == null )