changeset 141:c2ee8a717b73

move loading of standard modules to luan git-svn-id: https://luan-java.googlecode.com/svn/trunk@142 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 13 Jun 2014 16:37:49 +0000
parents f4ce03ff6b2f
children 05f8c21160ef
files src/luan/LuanState.java src/luan/lib/BasicLib.java src/luan/lib/PackageLib.java src/luan/lib/init.luan
diffstat 4 files changed, 40 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
diff -r f4ce03ff6b2f -r c2ee8a717b73 src/luan/LuanState.java
--- a/src/luan/LuanState.java	Fri Jun 13 15:04:29 2014 +0000
+++ b/src/luan/LuanState.java	Fri Jun 13 16:37:49 2014 +0000
@@ -131,7 +131,7 @@
 		return t.put(a[a.length-1],value);
 	}
 
-	public final void load(String modName,LuanFunction loader) throws LuanException {
+	private final void load(String modName,LuanFunction loader) throws LuanException {
 		preload.put(modName,loader);
 		Object mod = PackageLib.require(this,modName);
 		if( mod==null )
@@ -142,18 +142,8 @@
 	public static LuanState newStandard() {
 		try {
 			LuanState luan = LuanCompiler.newLuanState();
-			luan.load(BasicLib.NAME,BasicLib.LOADER);
 			luan.load(PackageLib.NAME,PackageLib.LOADER);
-			luan.load(MathLib.NAME,MathLib.LOADER);
-			luan.load(StringLib.NAME,StringLib.LOADER);
-			luan.load(TableLib.NAME,TableLib.LOADER);
-			luan.load(HtmlLib.NAME,HtmlLib.LOADER);
-			luan.load(BinaryLib.NAME,BinaryLib.LOADER);
-			luan.load(IoLib.NAME,IoLib.LOADER);
-			luan.load(ThreadLib.NAME,ThreadLib.LOADER);
 			BasicLib.do_java_resource(luan,"luan/lib/init.luan");
-			luan.preload.put(JavaLib.NAME,JavaLib.LOADER);
-			luan.preload.put(HttpLib.NAME,HttpLib.LOADER);
 			return luan;
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
diff -r f4ce03ff6b2f -r c2ee8a717b73 src/luan/lib/BasicLib.java
--- a/src/luan/lib/BasicLib.java	Fri Jun 13 15:04:29 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Fri Jun 13 16:37:49 2014 +0000
@@ -35,13 +35,11 @@
 				add( global, "assert_string", LuanState.class, String.class );
 				add( global, "assert_table", LuanState.class, LuanTable.class );
 				add( global, "do_file", LuanState.class, String.class );
-				add( global, "do_java_resource", LuanState.class, String.class );
 				add( global, "error", LuanState.class, Object.class );
 				add( global, "get_metatable", LuanState.class, Object.class );
 				add( global, "ipairs", LuanState.class, LuanTable.class );
 				add( global, "load", LuanState.class, String.class, String.class, Boolean.class, Boolean.class );
 				add( global, "load_file", LuanState.class, String.class );
-				add( global, "load_java_resource", LuanState.class, String.class );
 				add( global, "pairs", LuanState.class, LuanTable.class );
 				add( global, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class );
 				add( global, "raw_equal", Object.class, Object.class );
@@ -54,6 +52,8 @@
 				add( global, "to_string", LuanState.class, Object.class );
 				add( global, "type", Object.class );
 				global.put( "_VERSION", Luan.version );
+				add( module, "do_java_resource", LuanState.class, String.class );
+				add( module, "load_java_resource", LuanState.class, String.class );
 //				add( module, "new_luan" );
 				add( module, "values", new Object[0].getClass() );
 			} catch(NoSuchMethodException e) {
diff -r f4ce03ff6b2f -r c2ee8a717b73 src/luan/lib/PackageLib.java
--- a/src/luan/lib/PackageLib.java	Fri Jun 13 15:04:29 2014 +0000
+++ b/src/luan/lib/PackageLib.java	Fri Jun 13 16:37:49 2014 +0000
@@ -28,6 +28,7 @@
 			module.put("path","?.luan");
 			try {
 				add( global, "require", LuanState.class, String.class );
+				add( module, "get_loader", String.class );
 				add( module, "search_path", String.class, String.class );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
@@ -153,4 +154,15 @@
 		}
 	};
 
+
+	public static LuanFunction get_loader(String path)
+		throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException
+	{
+		int i = path.lastIndexOf('.');
+		String clsPath = path.substring(0,i);
+		String fld = path.substring(i+1);
+		Class cls = Class.forName(clsPath);
+		return (LuanFunction)cls.getField(fld).get(null);
+	}
+
 }
diff -r f4ce03ff6b2f -r c2ee8a717b73 src/luan/lib/init.luan
--- a/src/luan/lib/init.luan	Fri Jun 13 15:04:29 2014 +0000
+++ b/src/luan/lib/init.luan	Fri Jun 13 16:37:49 2014 +0000
@@ -1,9 +1,30 @@
---Io.stdout.write "this is init.luan\n"
+function Package.preload_java(name,path)
+	Package.preload[name] = Package.get_loader(path)
+end
+
+function Package.load_java(name,path)
+	Package.preload_java(name,path)
+	local mod = require(name)
+	_G[name] = mod
+	return mod
+end
+
+local Basic = Package.load_java("Basic","luan.lib.BasicLib.LOADER")
+local String = Package.load_java("String","luan.lib.StringLib.LOADER")
+local Table = Package.load_java("Table","luan.lib.TableLib.LOADER")
+local Io = Package.load_java("Io","luan.lib.IoLib.LOADER")
+Package.load_java("Math","luan.lib.MathLib.LOADER")
+Package.load_java("Html","luan.lib.HtmlLib.LOADER")
+Package.load_java("Thread","luan.lib.ThreadLib.LOADER")
+Package.load_java("Binary","luan.lib.BinaryLib.LOADER")
+Package.preload_java("Java","luan.lib.JavaLib.LOADER")
+Package.preload_java("Http","luan.lib.HttpLib.LOADER")
+
 
 function Io.print_to(out,...)
 	local list = {}
 	for _,v in Basic.values(...) do
-		list[#list+1] = to_string(v)
+		list[#list+1] = _G.to_string(v)
 		list[#list+1] = '\t'
 	end
 	if #list == 0 then
@@ -35,10 +56,10 @@
 	end
 	for line in console do
 		try
-			local fn = load(line,"stdin",true,true)
+			local fn = _G.load(line,"stdin",true,true)
 			Debug.print_if_something( fn() )
 		catch e do
-			print(e)
+			_G.print(e)
 		end
 	end
 end