changeset 149:f99fd64291b3

change load() to take env instead of using global git-svn-id: https://luan-java.googlecode.com/svn/trunk@150 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 16 Jun 2014 05:47:54 +0000
parents dfd0f33b584e
children f35c50027985
files src/luan/LuanState.java src/luan/LuanTable.java src/luan/interp/LuanCompiler.java src/luan/interp/LuanParser.java src/luan/interp/UpValue.java src/luan/lib/BasicLib.java src/luan/lib/PickleCon.java src/luan/lib/init.luan
diffstat 8 files changed, 31 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuanState.java	Mon Jun 16 04:29:33 2014 +0000
+++ b/src/luan/LuanState.java	Mon Jun 16 05:47:54 2014 +0000
@@ -152,7 +152,7 @@
 
 	public final Object eval(String cmd) {
 		try {
-			LuanFunction fn = BasicLib.load(this,cmd,"eval",true,true);
+			LuanFunction fn = BasicLib.load(this,cmd,"eval",null,true);
 			return call(fn);
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
--- a/src/luan/LuanTable.java	Mon Jun 16 04:29:33 2014 +0000
+++ b/src/luan/LuanTable.java	Mon Jun 16 05:47:54 2014 +0000
@@ -21,6 +21,13 @@
 
 	public LuanTable() {}
 
+	public LuanTable(LuanTable tbl) {
+		if( tbl.map != null )
+			this.map = new HashMap<Object,Object>(tbl.map);
+		if( tbl.list != null )
+			this.list = new ArrayList<Object>(tbl.list);
+	}
+
 	public LuanTable(List<Object> list) {
 		this.list = list;
 		this.map = new HashMap<Object,Object>();
--- a/src/luan/interp/LuanCompiler.java	Mon Jun 16 04:29:33 2014 +0000
+++ b/src/luan/interp/LuanCompiler.java	Mon Jun 16 05:47:54 2014 +0000
@@ -13,7 +13,7 @@
 public final class LuanCompiler {
 	private LuanCompiler() {}  // never
 
-	public static LuanFunction compileModule(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException {
+	private static LuanFunction compile(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException {
 		UpValue.Getter envGetter = new UpValue.EnvGetter();
 		LuanParser parser = new LuanParser(src,envGetter);
 		for( Map.Entry<Object,Object> entry : luan.global() ) {
@@ -33,8 +33,10 @@
 		};
 	}
 
-	public static LuanFunction compileGlobal(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException {
-		UpValue.Getter envGetter = UpValue.globalGetter;
+	public static LuanFunction compile(LuanState luan,LuanSource src,LuanTable env,boolean allowExpr) throws LuanException {
+		if( env==null )
+			return compile(luan,src,allowExpr);
+		UpValue.Getter envGetter = new UpValue.ValueGetter(env);
 		LuanParser parser = new LuanParser(src,envGetter);
 		FnDef fnDef = parse(luan,parser,allowExpr);
 		return new Closure((LuanStateImpl)luan,fnDef);
--- a/src/luan/interp/LuanParser.java	Mon Jun 16 04:29:33 2014 +0000
+++ b/src/luan/interp/LuanParser.java	Mon Jun 16 05:47:54 2014 +0000
@@ -104,7 +104,7 @@
 		this.source = source;
 		this.frame = new Frame(envGetter);
 		this.parser = new Parser(source);
-		this.interactive = envGetter==UpValue.globalGetter;
+		this.interactive = envGetter instanceof UpValue.ValueGetter;
 	}
 
 	void addVar(String name,Object value) {
--- a/src/luan/interp/UpValue.java	Mon Jun 16 04:29:33 2014 +0000
+++ b/src/luan/interp/UpValue.java	Mon Jun 16 05:47:54 2014 +0000
@@ -102,10 +102,4 @@
 		}
 	}
 
-	static final Getter globalGetter = new Getter() {
-		public UpValue get(LuanStateImpl luan) {
-			return new UpValue(luan.global());
-		}
-	};
-
 }
--- a/src/luan/lib/BasicLib.java	Mon Jun 16 04:29:33 2014 +0000
+++ b/src/luan/lib/BasicLib.java	Mon Jun 16 05:47:54 2014 +0000
@@ -36,7 +36,7 @@
 				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", 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 );
@@ -66,21 +66,18 @@
 		return Luan.type(obj);
 	}
 
-	public static LuanFunction load(LuanState luan,String text,String sourceName,Boolean useGlobal,Boolean allowExpr)
+	public static LuanFunction load(LuanState luan,String text,String sourceName,LuanTable env,Boolean allowExpr)
 		throws LuanException
 	{
 		if( allowExpr==null )
 			allowExpr = false;
-		if( useGlobal!=null && useGlobal )
-			return LuanCompiler.compileGlobal(luan,new LuanSource(sourceName,text),allowExpr);
-		else
-			return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text),allowExpr);
+		return LuanCompiler.compile(luan,new LuanSource(sourceName,text),env,allowExpr);
 	}
 
 	public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException {
 		try {
 			String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : IoLib.luanIo(luan,fileName).read_text();
-			return load(luan,src,fileName,false,false);
+			return load(luan,src,fileName,null,false);
 		} catch(IOException e) {
 			throw luan.exception(e);
 		}
--- a/src/luan/lib/PickleCon.java	Mon Jun 16 04:29:33 2014 +0000
+++ b/src/luan/lib/PickleCon.java	Mon Jun 16 05:47:54 2014 +0000
@@ -27,6 +27,7 @@
 	private final DataOutputStream out;
 	private final List<byte[]> binaries = new ArrayList<byte[]>();
 	String src;
+	private final LuanTable env;
 
 	PickleCon(LuanState luan,DataInputStream in,DataOutputStream out) {
 		this.in = in;
@@ -39,6 +40,7 @@
 			throw new RuntimeException(e);
 		}
 		this.ioModule = (LuanTable)luan.loaded().get("Io");
+		this.env = new LuanTable(luan.global());
 
 		this.out = out;
 	}
@@ -59,7 +61,7 @@
 		ioModule.put("_read_binary",_read_binary);
 		try {
 			src = in.readUTF();
-			LuanFunction fn = BasicLib.load(luan,src,"pickle-reader",true,false);
+			LuanFunction fn = BasicLib.load(luan,src,"pickle-reader",env,false);
 			return luan.call(fn);
 		} finally {
 			ioModule.put("_binaries",null);
--- a/src/luan/lib/init.luan	Mon Jun 16 04:29:33 2014 +0000
+++ b/src/luan/lib/init.luan	Mon Jun 16 05:47:54 2014 +0000
@@ -50,14 +50,23 @@
 	end
 end
 
+function Table.clone(tbl)
+	local new = {}
+	for k,v in _G.pairs(tbl) do
+		new[k] = v
+	end
+	return new
+end
+
 function Debug.debug(prompt)
 	prompt = prompt or "luan_debug> "
 	local function console()
 		return Io.read_console_line(prompt)
 	end
+	local env = Table.clone(_G)
 	for line in console do
 		try
-			local fn = _G.load(line,"stdin",true,true)
+			local fn = _G.load(line,"stdin",env,true)
 			Debug.print_if_something( fn() )
 		catch e do
 			_G.print(e)