changeset 320:fed1893821bf

remove global namespace git-svn-id: https://luan-java.googlecode.com/svn/trunk@321 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 06 Feb 2015 21:54:41 +0000
parents f6db49c294a7
children 7f7708e8fdd4
files core/src/luan/LuanState.java core/src/luan/cmd_line.luan core/src/luan/impl/JavaStmt.java core/src/luan/impl/LuanCompiler.java core/src/luan/impl/LuanParser.java core/src/luan/modules/Binary.luan core/src/luan/modules/Html.luan core/src/luan/modules/Io.luan core/src/luan/modules/JavaLuan.java core/src/luan/modules/Luan.luan core/src/luan/modules/Math.luan core/src/luan/modules/Package.luan core/src/luan/modules/String.luan core/src/luan/modules/Table.luan core/src/luan/modules/Thread.luan core/src/luan/modules/Time.luan logging/src/luan/modules/logging/Logging.luan lucene/src/luan/modules/lucene/Lucene.luan mail/src/luan/modules/mail/Mail.luan scripts/build-luan.sh scripts/mmake.luan web/src/luan/modules/web/Http.luan web/src/luan/modules/web/Web_server.luan
diffstat 23 files changed, 85 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/LuanState.java
--- a/core/src/luan/LuanState.java	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/LuanState.java	Fri Feb 06 21:54:41 2015 +0000
@@ -21,14 +21,10 @@
 	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
 
 	private LuanTableImpl registry;
-	private LuanTableImpl global;
 	private LuanTableImpl metatable;  // generic metatable
 
 	protected LuanState() {
 		registry = new LuanTableImpl();
-		global = new LuanTableImpl();
-		global.put("_G",global);
-		global.put("java",JavaLuan.javaFn);
 		metatable = newMetatable();
 	}
 
@@ -36,7 +32,6 @@
 
 	@Override public void deepenClone(LuanState clone,DeepCloner cloner) {
 		clone.registry = cloner.deepClone(registry);
-		clone.global = cloner.deepClone(global);
 		clone.metatable = cloner.deepClone(metatable);
 	}
 
@@ -47,33 +42,24 @@
 		return registry;
 	}
 
-	public final LuanTable global() {
-		return global;
-	}
-
 	public static LuanState newStandard() {
 		try {
 			LuanState luan = LuanCompiler.newLuanState();
 			PackageLuan.require(luan,"luan:Luan");
 			PackageLuan.require(luan,"luan:Io");
-//			BasicLuan.do_file(luan,"classpath:luan/init.luan");
 			return luan;
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
 		}
 	}
 
-	public final Object eval(String cmd) {
+	public final Object eval(String cmd) throws LuanException {
 		return eval(cmd,new LuanTableImpl());
 	}
 
-	public final Object eval(String cmd,LuanTable env) {
-		try {
-			LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true);
-			return call(fn);
-		} catch(LuanException e) {
-			throw new RuntimeException(e);
-		}
+	public final Object eval(String cmd,LuanTable env) throws LuanException {
+		LuanFunction fn = BasicLuan.load(this,cmd,"eval",env,true);
+		return call(fn);
 	}
 
 	public final LuanTable getMetatable(Object obj) {
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/cmd_line.luan
--- a/core/src/luan/cmd_line.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/cmd_line.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,8 +1,13 @@
+import "luan:Luan"
 import "luan:String"
 import "luan:Table"
 import "luan:Io"
 import "luan:Debug"
 
+local ipairs = Luan.ipairs
+local load = Luan.load
+local load_file = Luan.load_file
+
 
 local standalone_usage = [=[
 usage: java luan.Luan [options] [script [args]]
@@ -63,13 +68,13 @@
 if showVersion then print(_VERSION) end
 if i <= #args then
 	local file = args[i]
-	_G.arg = {}
+	local arg = {}
 	for j,v in ipairs(args) do
-		_G.arg[j-i] = v
+		arg[j-i] = v
 	end
 	try
 		local main_file = load_file("file:"..file)
-		main_file( Table.unpack(_G.arg) )
+		main_file( Table.unpack(arg) )
 	catch e do
 		Io.print_to(Io.stderr, e )
 	end
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/impl/JavaStmt.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/impl/JavaStmt.java	Fri Feb 06 21:54:41 2015 +0000
@@ -0,0 +1,12 @@
+package luan.impl;
+
+import luan.LuanException;
+import luan.modules.JavaLuan;
+
+
+final class JavaStmt implements Stmt {
+
+	@Override public void eval(LuanStateImpl luan) throws LuanException {
+		JavaLuan.java(luan);
+	}
+}
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/impl/LuanCompiler.java
--- a/core/src/luan/impl/LuanCompiler.java	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/impl/LuanCompiler.java	Fri Feb 06 21:54:41 2015 +0000
@@ -19,11 +19,6 @@
 			env = Luan.newTable();
 		UpValue.Getter envGetter = new UpValue.ValueGetter(env);
 		LuanParser parser = new LuanParser(src,envGetter);
-		for( Map.Entry<Object,Object> entry : luan.global() ) {
-			Object key = entry.getKey();
-			if( key instanceof String )
-				parser.addVar( (String)key, entry.getValue() );
-		}
 		FnDef fnDef = parse(luan,parser,allowExpr);
 		final LuanStateImpl luanImpl = (LuanStateImpl)luan;
 		final Closure c = new Closure(luanImpl,fnDef);
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/impl/LuanParser.java
--- a/core/src/luan/impl/LuanParser.java	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/impl/LuanParser.java	Fri Feb 06 21:54:41 2015 +0000
@@ -250,6 +250,7 @@
 			|| (stmt=FunctionStmt()) != null
 			|| (stmt=LocalFunctionStmt()) != null
 			|| (stmt=ImportStmt()) != null
+			|| (stmt=JavaStmt()) != null
 			|| (stmt=BreakStmt()) != null
 			|| (stmt=ForStmt()) != null
 			|| (stmt=TryStmt()) != null
@@ -387,6 +388,13 @@
 		return true;
 	}
 
+	private Stmt JavaStmt() throws ParseException {
+		parser.begin();
+		if( !Keyword("java",In.NOTHING) )
+			return parser.failure(null);
+		return parser.success( new JavaStmt() );
+	}
+
 	private Stmt BreakStmt() throws ParseException {
 		parser.begin();
 		if( !Keyword("break",In.NOTHING) )
@@ -1148,6 +1156,7 @@
 		"if",
 		"import",
 		"in",
+		"java",
 		"local",
 		"nil",
 		"not",
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Binary.luan
--- a/core/src/luan/modules/Binary.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Binary.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,4 +1,4 @@
-java()
+java
 import "java:luan.modules.BinaryLuan"
 
 byte = BinaryLuan.byte_
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Html.luan
--- a/core/src/luan/modules/Html.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Html.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,14 +1,19 @@
-java()
+java
 import "java:luan.modules.HtmlLuan"
 
 encode = HtmlLuan.encode
 parse = HtmlLuan.parse
-_ENV.to_string = HtmlLuan.to_string
+to_string = HtmlLuan.to_string
 
 
 
 -- extras
 
+import "luan:Luan"
+
+local ipairs = Luan.ipairs
+local type = Luan.type
+
 function process_url_tags(html)
 	for i, v in ipairs(html) do
 		if type(v) == "table" and v.type == "tag" then
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Io.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,4 +1,4 @@
-java()
+java
 import "java:luan.modules.IoLuan"
 import "java:java.lang.System"
 
@@ -11,10 +11,11 @@
 stderr = IoLuan.textWriter(System.err)
 
 
-import "luan:Package"
 import "luan:Luan"
 import "luan:Table"
 
+local to_string = Luan.to_string
+
 function print_to(out,...)
 	local list = {}
 	for _,v in Luan.values(...) do
@@ -29,7 +30,6 @@
 	end
 end
 
-function Luan.print(...)
+function print(...)
 	print_to(stdout,...)
 end
-Package.global(Luan,"print")
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/JavaLuan.java
--- a/core/src/luan/modules/JavaLuan.java	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/JavaLuan.java	Fri Feb 06 21:54:41 2015 +0000
@@ -32,15 +32,6 @@
 		luan.currentEnvironment().setJava();
 	}
 
-	public static final LuanFunction javaFn;
-	static {
-		try {
-			javaFn = new LuanJavaFunction(JavaLuan.class.getMethod("java",LuanState.class),null);
-		} catch(NoSuchMethodException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
 	private static void checkJava(LuanState luan) throws LuanException {
 		if( !luan.currentEnvironment().hasJava() )
 			throw luan.exception("Java isn't allowed");
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Luan.luan
--- a/core/src/luan/modules/Luan.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Luan.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,4 +1,4 @@
-java()
+java
 import "java:luan.modules.BasicLuan"
 
 assert = BasicLuan.assert_
@@ -29,36 +29,5 @@
 type = BasicLuan.type
 values = BasicLuan.values
 
-
-import "luan:Package"
-local Basic = _ENV
-
-Package.global(Package,"require")
-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,"assert_integer")
-Package.global(Basic,"assert_long")
-Package.global(Basic,"do_file")
-Package.global(Basic,"error")
-Package.global(Basic,"get_metatable")
-Package.global(Basic,"ipairs")
-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_boolean")
-Package.global(Basic,"to_number")
-Package.global(Basic,"to_string")
-Package.global(Basic,"type")
-
-do_file "classpath:luan/version.luan"
+import "classpath:luan/Version"
+VERSION = Version.VERSION
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Math.luan
--- a/core/src/luan/modules/Math.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Math.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,5 +1,8 @@
-java()
+java
+import "luan:Package"
 import "java:luan.modules.MathLuan"
+
+local require = Package.require
 local JavaMath = require "java:java.lang.Math"
 
 abs = MathLuan.abs
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Package.luan
--- a/core/src/luan/modules/Package.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Package.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,14 +1,6 @@
-java()
+java
 import "java:luan.modules.PackageLuan"
 
 loaded = PackageLuan.loaded()
 require = PackageLuan.requireFn
 load = PackageLuan.load
-
-function global(module,fn_name)
-	local function fn(...)
-		return module[fn_name](...)
-	end
-	_G[fn_name] = fn
-	return fn
-end
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/String.luan
--- a/core/src/luan/modules/String.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/String.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,4 +1,4 @@
-java()
+java
 import "java:luan.modules.StringLuan"
 
 unicode = StringLuan.unicode
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Table.luan
--- a/core/src/luan/modules/Table.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Table.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,4 +1,4 @@
-java()
+java
 import "java:luan.modules.TableLuan"
 
 clone = TableLuan.clone
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Thread.luan
--- a/core/src/luan/modules/Thread.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Thread.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,4 +1,4 @@
-java()
+java
 import "java:luan.modules.ThreadLuan"
 
 fork = ThreadLuan.fork
diff -r f6db49c294a7 -r fed1893821bf core/src/luan/modules/Time.luan
--- a/core/src/luan/modules/Time.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/core/src/luan/modules/Time.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,6 +1,6 @@
 -- incomplete, will add as needed
 
-java()
+java
 import "luan:String"
 import "luan:Table"
 import "java:java.lang.System"
diff -r f6db49c294a7 -r fed1893821bf logging/src/luan/modules/logging/Logging.luan
--- a/logging/src/luan/modules/logging/Logging.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/logging/src/luan/modules/logging/Logging.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,4 +1,4 @@
-java()
+java
 import "java:org.apache.log4j.Logger"
 import "java:org.apache.log4j.EnhancedPatternLayout"
 import "java:org.apache.log4j.ConsoleAppender"
diff -r f6db49c294a7 -r fed1893821bf lucene/src/luan/modules/lucene/Lucene.luan
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,4 +1,5 @@
-java()
+java
+import "luan:Luan"
 import "luan:Table"
 import "java:luan.modules.lucene.LuceneIndex"
 import "java:org.apache.lucene.index.Term"
@@ -9,6 +10,8 @@
 import "java:org.apache.lucene.search.Sort"
 import "java:org.apache.lucene.search.SortField"
 
+local pairs = Luan.pairs
+
 
 function Index(indexDir)
 	local index = {}
diff -r f6db49c294a7 -r fed1893821bf mail/src/luan/modules/mail/Mail.luan
--- a/mail/src/luan/modules/mail/Mail.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/mail/src/luan/modules/mail/Mail.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,7 +1,10 @@
-java()
+java
+import "luan:Luan"
 import "java:java.lang.System"
 import "java:luan.modules.mail.SmtpCon"
 
+local assert_table = Luan.assert_table
+
 
 System.setProperty( "mail.mime.charset", "UTF-8" )
 
diff -r f6db49c294a7 -r fed1893821bf scripts/build-luan.sh
--- a/scripts/build-luan.sh	Thu Feb 05 21:55:49 2015 +0000
+++ b/scripts/build-luan.sh	Fri Feb 06 21:54:41 2015 +0000
@@ -18,7 +18,7 @@
 mkdir $LUAN_BUILD/luan/jars
 
 cd $LUAN_HOME
-echo "_G._VERSION = 'Luan $VERSION'" >core/src/luan/version.luan
+echo "VERSION = 'Luan $VERSION'" >core/src/luan/Version.luan
 
 cd $LUAN_HOME
 SRC=core/src
diff -r f6db49c294a7 -r fed1893821bf scripts/mmake.luan
--- a/scripts/mmake.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/scripts/mmake.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,28 +1,33 @@
+import "luan:Luan"
 import "luan:Table"
 import "luan:Io"
 import "luan:String"
 import "luan:Time"
 
+local ipairs = Luan.ipairs
+local print = Io.print
+
+
 compiler = Table.concat( { "javac -g -encoding UTF8", ... }, " " )
 
 function mmake(dir)
-	local java = {}
+	local javas = {}
 	local dirs = {}
 	for _, file in ipairs(dir.children()) do
 		local name = file.name()
 		if name.match ".java$" ~= nil then
-			java[#java+1] = name.sub(1,-6)
+			javas[#javas+1] = name.sub(1,-6)
 		end
 		if file.is_directory() and mmake(file) then
 			dirs[#dirs+1] = name
 		end
 	end
-	if #java == 0 and #dirs == 0 then
+	if #javas == 0 and #dirs == 0 then
 		return false;
 	end
 	local out = dir.child("Makefile").text_writer()
 	out.write( header() )
-	for _, s in ipairs(java) do
+	for _, s in ipairs(javas) do
 		out.write( "\\\n\t\t",  s , ".class" )
 	end
 	for _, s in ipairs(dirs) do
diff -r f6db49c294a7 -r fed1893821bf web/src/luan/modules/web/Http.luan
--- a/web/src/luan/modules/web/Http.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/web/src/luan/modules/web/Http.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -1,7 +1,14 @@
-java()
+java
+import "luan:Package"
+import "luan:Luan"
 import "luan:Table"
 import "java:luan.modules.web.LuanHandler"
 
+local require = Package.require
+local ipairs = Luan.ipairs
+local to_string = Luan.to_string
+
+
 function new_luan_handler()
 	return LuanHandler.new()
 end
diff -r f6db49c294a7 -r fed1893821bf web/src/luan/modules/web/Web_server.luan
--- a/web/src/luan/modules/web/Web_server.luan	Thu Feb 05 21:55:49 2015 +0000
+++ b/web/src/luan/modules/web/Web_server.luan	Fri Feb 06 21:54:41 2015 +0000
@@ -4,7 +4,7 @@
 import "luan:logging/Logging"  -- initialize logging
 import "luan:web/Http"
 
-java()
+java
 import "java:org.eclipse.jetty.server.Server"
 import "java:org.eclipse.jetty.server.NCSARequestLog"
 import "java:org.eclipse.jetty.server.handler.DefaultHandler"