diff src/luan/LuanState.java @ 108:3c404a296995

make Package module more standard; return _ENV by default; add "import" statement; git-svn-id: https://luan-java.googlecode.com/svn/trunk@109 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 23 May 2014 03:21:54 +0000
parents 1f8b6edc2b08
children 2428ecfed375
line wrap: on
line diff
--- a/src/luan/LuanState.java	Mon May 19 09:17:57 2014 +0000
+++ b/src/luan/LuanState.java	Fri May 23 03:21:54 2014 +0000
@@ -17,13 +17,12 @@
 
 
 public abstract class LuanState implements DeepCloneable<LuanState> {
-	public static final String _G = "_G";
-
 	public final LuanBit JAVA = bit(LuanElement.JAVA);
 
+	private LuanTable global;
 	private LuanTable loaded;
 	private LuanTable preload;
-	private final List<String> defaultMods;
+	private LuanTable searchers;
 
 	public InputStream in = System.in;
 	public PrintStream out = System.out;
@@ -33,15 +32,16 @@
 	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
 
 	protected LuanState() {
+		global = new LuanTable();
+		global.put("_G",global);
 		loaded = new LuanTable();
 		preload = new LuanTable();
-		defaultMods = new ArrayList<String>();
+		searchers = new LuanTable();
 		mtGetters = new ArrayList<MetatableGetter>();
 	}
 
 	protected LuanState(LuanState luan) {
 		mtGetters = new ArrayList<MetatableGetter>(luan.mtGetters);
-		defaultMods = new ArrayList<String>(luan.defaultMods);
 	}
 
 	public final LuanState deepClone() {
@@ -49,12 +49,18 @@
 	}
 
 	@Override public void deepenClone(LuanState clone,DeepCloner cloner) {
+		clone.global = cloner.deepClone(global);
 		clone.loaded = cloner.deepClone(loaded);
 		clone.preload = cloner.deepClone(preload);
+		clone.searchers = cloner.deepClone(searchers);
 	}
 
 	public abstract LuanTable currentEnvironment();
 
+	public final LuanTable global() {
+		return global;
+	}
+
 	public final LuanTable loaded() {
 		return loaded;
 	}
@@ -63,6 +69,10 @@
 		return preload;
 	}
 
+	public final LuanTable searchers() {
+		return searchers;
+	}
+
 	public final Object get(String name) {
 		String[] a = name.split("\\.");
 		LuanTable t = loaded;
@@ -89,23 +99,10 @@
 
 	public final void load(String modName,LuanFunction loader) throws LuanException {
 		preload.put(modName,loader);
-		defaultMods.add(modName);
-		PackageLib.require(this,modName);
-	}
-
-	public final LuanTable newEnvironment() throws LuanException {
-		LuanTable env = new LuanTable();
-		for( String modName : defaultMods ) {
-			PackageLib.require(this,modName,env);
-			LuanTable mod = (LuanTable)loaded.get(modName);
-			LuanTable global = (LuanTable)mod.get(_G);
-			if( global != null ) {
-				for( Map.Entry<Object,Object> entry : global ) {
-					env.put( entry.getKey(), entry.getValue() );
-				}
-			}
-		}
-		return env;
+		Object mod = PackageLib.require(this,modName);
+		if( mod==null )
+			throw new RuntimeException();
+		global.put(modName,mod);
 	}
 
 	public static LuanState newStandard() {
@@ -124,8 +121,8 @@
 		}
 	}
 
-	public final Object[] eval(String cmd,String sourceName,LuanTable env) throws LuanException {
-		LuanFunction fn = BasicLib.load(this,cmd,sourceName,env);
+	public final Object[] eval(String cmd,String sourceName,boolean interactive) throws LuanException {
+		LuanFunction fn = BasicLib.load(this,cmd,sourceName,interactive);
 		return JAVA.call(fn,null);
 	}