changeset 150:f35c50027985

make load() more consistent git-svn-id: https://luan-java.googlecode.com/svn/trunk@151 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 16 Jun 2014 10:11:48 +0000
parents f99fd64291b3
children c9100f29fae0
files src/luan/LuanState.java src/luan/interp/LuanCompiler.java src/luan/lib/PickleCon.java src/luan/lib/init.luan
diffstat 4 files changed, 7 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuanState.java	Mon Jun 16 05:47:54 2014 +0000
+++ b/src/luan/LuanState.java	Mon Jun 16 10:11:48 2014 +0000
@@ -152,7 +152,7 @@
 
 	public final Object eval(String cmd) {
 		try {
-			LuanFunction fn = BasicLib.load(this,cmd,"eval",null,true);
+			LuanFunction fn = BasicLib.load(this,cmd,"eval",new LuanTable(),true);
 			return call(fn);
 		} catch(LuanException e) {
 			throw new RuntimeException(e);
--- a/src/luan/interp/LuanCompiler.java	Mon Jun 16 05:47:54 2014 +0000
+++ b/src/luan/interp/LuanCompiler.java	Mon Jun 16 10:11:48 2014 +0000
@@ -13,8 +13,8 @@
 public final class LuanCompiler {
 	private LuanCompiler() {}  // never
 
-	private static LuanFunction compile(LuanState luan,LuanSource src,boolean allowExpr) throws LuanException {
-		UpValue.Getter envGetter = new UpValue.EnvGetter();
+	public static LuanFunction compile(LuanState luan,LuanSource src,LuanTable env,boolean allowExpr) throws LuanException {
+		UpValue.Getter envGetter = env!=null ? new UpValue.ValueGetter(env) : new UpValue.EnvGetter();
 		LuanParser parser = new LuanParser(src,envGetter);
 		for( Map.Entry<Object,Object> entry : luan.global() ) {
 			Object key = entry.getKey();
@@ -22,6 +22,8 @@
 				parser.addVar( (String)key, entry.getValue() );
 		}
 		FnDef fnDef = parse(luan,parser,allowExpr);
+		if( env != null )
+			return new Closure((LuanStateImpl)luan,fnDef);
 		final Closure c = new Closure((LuanStateImpl)luan,fnDef);
 		return new LuanFunction() {
 			@Override public Object call(LuanState luan,Object[] args) throws LuanException {
@@ -33,15 +35,6 @@
 		};
 	}
 
-	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);
-	}
-
 	private static FnDef parse(LuanState luan,LuanParser parser,boolean allowExpr) throws LuanException {
 		try {
 			if( allowExpr ) {
--- a/src/luan/lib/PickleCon.java	Mon Jun 16 05:47:54 2014 +0000
+++ b/src/luan/lib/PickleCon.java	Mon Jun 16 10:11:48 2014 +0000
@@ -27,7 +27,7 @@
 	private final DataOutputStream out;
 	private final List<byte[]> binaries = new ArrayList<byte[]>();
 	String src;
-	private final LuanTable env;
+	private final LuanTable env = new LuanTable();
 
 	PickleCon(LuanState luan,DataInputStream in,DataOutputStream out) {
 		this.in = in;
@@ -40,7 +40,6 @@
 			throw new RuntimeException(e);
 		}
 		this.ioModule = (LuanTable)luan.loaded().get("Io");
-		this.env = new LuanTable(luan.global());
 
 		this.out = out;
 	}
--- a/src/luan/lib/init.luan	Mon Jun 16 05:47:54 2014 +0000
+++ b/src/luan/lib/init.luan	Mon Jun 16 10:11:48 2014 +0000
@@ -50,20 +50,12 @@
 	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)
+	local env = {}
 	for line in console do
 		try
 			local fn = _G.load(line,"stdin",env,true)