changeset 163:3c95a2291d64

make all module fns non-global and make global fns call module fns git-svn-id: https://luan-java.googlecode.com/svn/trunk@164 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 20 Jun 2014 09:32:36 +0000
parents 01e9707a2fb0
children 78ba371ea1e9
files src/luan/LuanState.java src/luan/lib/BasicLib.java src/luan/lib/JavaLib.java src/luan/lib/PackageLib.java src/luan/lib/StringLib.java src/luan/lib/init.luan
diffstat 6 files changed, 67 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuanState.java	Fri Jun 20 08:53:44 2014 +0000
+++ b/src/luan/LuanState.java	Fri Jun 20 09:32:36 2014 +0000
@@ -72,6 +72,7 @@
 	protected LuanState() {
 		global = new LuanTable();
 		global.put("_G",global);
+		global.put( "_VERSION", Luan.version );
 		loaded = new LuanTable();
 		preload = new LuanTable();
 		searchers = new LuanTable();
--- a/src/luan/lib/BasicLib.java	Fri Jun 20 08:53:44 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Fri Jun 20 09:32:36 2014 +0000
@@ -24,32 +24,30 @@
 	public static final LuanFunction LOADER = new LuanFunction() {
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = new LuanTable();
-			LuanTable global = luan.global();
 			try {
-				global.put( "assert", new LuanJavaFunction(BasicLib.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) );
-				add( global, "assert_boolean", LuanState.class, Boolean.TYPE );
-				add( global, "assert_nil", LuanState.class, Object.class );
-				add( global, "assert_number", LuanState.class, Number.class );
-				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, "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, LuanTable.class, Boolean.class );
-				add( global, "load_file", 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 );
-				add( global, "raw_get", LuanTable.class, Object.class );
-				add( global, "raw_len", LuanState.class, Object.class );
-				add( global, "raw_set", LuanTable.class, Object.class, Object.class );
-				add( global, "repr", LuanState.class, Object.class );
-				add( global, "set_metatable", LuanTable.class, LuanTable.class );
-				add( global, "to_number", Object.class, Integer.class );
-				add( global, "to_string", LuanState.class, Object.class );
-				add( global, "type", Object.class );
-				global.put( "_VERSION", Luan.version );
+				module.put( "assert", new LuanJavaFunction(BasicLib.class.getMethod("assert_",LuanState.class,Object.class,String.class),null) );
+				add( module, "assert_boolean", LuanState.class, Boolean.TYPE );
+				add( module, "assert_nil", LuanState.class, Object.class );
+				add( module, "assert_number", LuanState.class, Number.class );
+				add( module, "assert_string", LuanState.class, String.class );
+				add( module, "assert_table", LuanState.class, LuanTable.class );
+				add( module, "do_file", LuanState.class, String.class );
+				add( module, "error", LuanState.class, Object.class );
+				add( module, "get_metatable", LuanState.class, Object.class );
+				add( module, "ipairs", LuanState.class, LuanTable.class );
+				add( module, "load", LuanState.class, String.class, String.class, LuanTable.class, Boolean.class );
+				add( module, "load_file", LuanState.class, String.class );
+				add( module, "pairs", LuanState.class, LuanTable.class );
+				add( module, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class );
+				add( module, "raw_equal", Object.class, Object.class );
+				add( module, "raw_get", LuanTable.class, Object.class );
+				add( module, "raw_len", LuanState.class, Object.class );
+				add( module, "raw_set", LuanTable.class, Object.class, Object.class );
+				add( module, "repr", LuanState.class, Object.class );
+				add( module, "set_metatable", LuanTable.class, LuanTable.class );
+				add( module, "to_number", Object.class, Integer.class );
+				add( module, "to_string", LuanState.class, Object.class );
+				add( module, "type", Object.class );
 				add( module, "values", new Object[0].getClass() );
 			} catch(NoSuchMethodException e) {
 				throw new RuntimeException(e);
--- a/src/luan/lib/JavaLib.java	Fri Jun 20 08:53:44 2014 +0000
+++ b/src/luan/lib/JavaLib.java	Fri Jun 20 09:32:36 2014 +0000
@@ -32,7 +32,6 @@
 		@Override public Object call(LuanState luan,Object[] args) {
 			luan.addMetatableGetter(mg);
 			LuanTable module = new LuanTable();
-			LuanTable global = luan.global();
 			try {
 				module.put( "class", new LuanJavaFunction(JavaLib.class.getMethod("getClass",LuanState.class,String.class),null) );
 				add( module, "proxy", LuanState.class, Static.class, LuanTable.class, Object.class );
--- a/src/luan/lib/PackageLib.java	Fri Jun 20 08:53:44 2014 +0000
+++ b/src/luan/lib/PackageLib.java	Fri Jun 20 09:32:36 2014 +0000
@@ -20,13 +20,12 @@
 	public static final LuanFunction LOADER = new LuanFunction() {
 		@Override public Object call(LuanState luan,Object[] args) {
 			LuanTable module = new LuanTable();
-			LuanTable global = luan.global();
 			module.put("loaded",luan.loaded());
 			module.put("preload",luan.preload());
 			module.put("path","?.luan;java:luan/modules/?.luan");
 			module.put("jpath",jpath);
 			try {
-				add( global, "require", LuanState.class, String.class );
+				add( module, "require", LuanState.class, String.class );
 				add( module, "load_lib", String.class );
 				add( module, "search_path", String.class, String.class );
 				add( module, "search", LuanState.class, String.class );
--- a/src/luan/lib/StringLib.java	Fri Jun 20 08:53:44 2014 +0000
+++ b/src/luan/lib/StringLib.java	Fri Jun 20 09:32:36 2014 +0000
@@ -60,7 +60,7 @@
 	};
 
 	public static Object __index(LuanState luan,final String s,Object key) throws LuanException {
-		LuanTable mod = (LuanTable)luan.global().get("String");
+		LuanTable mod = (LuanTable)luan.loaded().get("String");
 		if( mod!=null ) {
 			Object obj = mod.get(key);
 			if( obj instanceof LuanFunction ) {
--- a/src/luan/lib/init.luan	Fri Jun 20 08:53:44 2014 +0000
+++ b/src/luan/lib/init.luan	Fri Jun 20 09:32:36 2014 +0000
@@ -1,3 +1,13 @@
+function Package.global(module,fn_name)
+	local function fn(...)
+		return module[fn_name](...)
+	end
+	_G[fn_name] = fn
+	return fn
+end
+
+local require = Package.global(Package,"require")
+
 function Package.global_import(name)
 	local mod = require(name)
 	_G[name] = mod
@@ -5,6 +15,30 @@
 end
 
 local Basic = Package.global_import("Basic","luan.lib.BasicLib.LOADER")
+Package.global(Basic,"assert")
+Package.global(Basic,"assert_boolean")
+Package.global(Basic,"assert_nil")
+Package.global(Basic,"assert_number")
+Package.global(Basic,"assert_string")
+Package.global(Basic,"assert_table")
+Package.global(Basic,"do_file")
+Package.global(Basic,"error")
+Package.global(Basic,"get_metatable")
+Package.global(Basic,"ipairs")
+local load = Package.global(Basic,"load")
+Package.global(Basic,"load_file")
+Package.global(Basic,"pairs")
+Package.global(Basic,"range")
+Package.global(Basic,"raw_equal")
+Package.global(Basic,"raw_get")
+Package.global(Basic,"raw_len")
+Package.global(Basic,"raw_set")
+Package.global(Basic,"repr")
+Package.global(Basic,"set_metatable")
+Package.global(Basic,"to_number")
+local to_string = Package.global(Basic,"to_string")
+Package.global(Basic,"type")
+
 local String = Package.global_import("String","luan.lib.StringLib.LOADER")
 
 -- improved global_import
@@ -27,7 +61,7 @@
 function Io.print_to(out,...)
 	local list = {}
 	for _,v in Basic.values(...) do
-		list[#list+1] = _G.to_string(v)
+		list[#list+1] = to_string(v)
 		list[#list+1] = '\t'
 	end
 	if #list == 0 then
@@ -38,9 +72,10 @@
 	end
 end
 
-function _G.print(...)
+function Basic.print(...)
 	Io.print_to(Io.stdout,...)
 end
+local print = Package.global(Basic,"print")
 
 local Debug = {}
 Package.loaded.Debug = Debug
@@ -48,7 +83,7 @@
 
 function Debug.print_if_something(...)
 	if Table.pack(...).n > 0 then
-		_G.print(...)
+		print(...)
 	end
 end
 
@@ -60,14 +95,14 @@
 	local env = {}
 	for line in console do
 		try
-			local fn = _G.load(line,"stdin",env,true)
+			local fn = load(line,"stdin",env,true)
 			Debug.print_if_something( fn() )
 		catch e do
-			_G.print(e)
+			print(e)
 		end
 	end
 end
 
 
 -- import modules
-_G.Reactionary = require "Reactionary"
+Package.global_import("Reactionary")