changeset 301:a6bf8ff720f8

add java security git-svn-id: https://luan-java.googlecode.com/svn/trunk@302 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 17 Dec 2014 12:35:57 +0000
parents 9fb523472035
children 2f8938fc518c
files core/src/luan/LuanState.java core/src/luan/impl/Closure.java core/src/luan/impl/LuanStateImpl.java core/src/luan/modules/IoLuan.java core/src/luan/modules/JavaLuan.java core/src/luan/modules/PackageLuan.java core/src/luan/modules/Thread.luan core/src/luan/modules/ThreadLuan.java
diffstat 8 files changed, 56 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanState.java	Tue Dec 16 13:13:33 2014 +0000
+++ b/core/src/luan/LuanState.java	Wed Dec 17 12:35:57 2014 +0000
@@ -40,6 +40,7 @@
 	}
 
 	public abstract LuanTable currentEnvironment();
+	public abstract LuanSource currentSource();
 
 	public final LuanTable registry() {
 		return registry;
--- a/core/src/luan/impl/Closure.java	Tue Dec 16 13:13:33 2014 +0000
+++ b/core/src/luan/impl/Closure.java	Wed Dec 17 12:35:57 2014 +0000
@@ -10,7 +10,7 @@
 
 
 final class Closure extends LuanFunction implements DeepCloneable<Closure> {
-	private final FnDef fnDef;
+	final FnDef fnDef;
 	private UpValue[] upValues;
 
 	Closure(LuanStateImpl luan,FnDef fnDef) throws LuanException {
--- a/core/src/luan/impl/LuanStateImpl.java	Tue Dec 16 13:13:33 2014 +0000
+++ b/core/src/luan/impl/LuanStateImpl.java	Wed Dec 17 12:35:57 2014 +0000
@@ -10,6 +10,7 @@
 import luan.LuanFunction;
 import luan.LuanException;
 import luan.LuanElement;
+import luan.LuanSource;
 import luan.DeepCloner;
 
 
@@ -113,4 +114,10 @@
 		return (LuanTable)frame.closure.upValues()[0].get();
 	}
 
+	@Override public LuanSource currentSource(){
+		if( frame==null )
+			return null;
+		return frame.closure.fnDef.se().source;
+	}
+
 }
--- a/core/src/luan/modules/IoLuan.java	Tue Dec 16 13:13:33 2014 +0000
+++ b/core/src/luan/modules/IoLuan.java	Wed Dec 17 12:35:57 2014 +0000
@@ -333,7 +333,7 @@
 
 		private LuanFile(LuanState luan,File file) throws LuanException {
 			this(file);
-			check(luan,"file",file.toString());
+			check(luan,"file:"+file.toString());
 		}
 
 		private LuanFile(File file) {
@@ -424,7 +424,7 @@
 		if( name.contains("//") )
 			return null;
 		String path = name;
-		check(luan,"classpath",path);
+		check(luan,"classpath:"+path);
 		URL url;
 		if( !path.contains("#") ) {
 			url = ClassLoader.getSystemResource(path);
@@ -595,15 +595,15 @@
 	// security
 
 	public interface Security {
-		public void check(LuanState luan,String scheme,String name) throws LuanException;
+		public void check(LuanState luan,String name) throws LuanException;
 	}
 
 	private static String SECURITY_KEY = "Io.Security";
 
-	private static void check(LuanState luan,String scheme,String name) throws LuanException {
+	private static void check(LuanState luan,String name) throws LuanException {
 		Security s = (Security)luan.registry().get(SECURITY_KEY);
 		if( s!=null )
-			s.check(luan,scheme,name);
+			s.check(luan,name);
 	}
 
 	public static void setSecurity(LuanState luan,Security s) {
--- a/core/src/luan/modules/JavaLuan.java	Tue Dec 16 13:13:33 2014 +0000
+++ b/core/src/luan/modules/JavaLuan.java	Wed Dec 17 12:35:57 2014 +0000
@@ -27,7 +27,8 @@
 
 public final class JavaLuan {
 
-	public static void java(LuanState luan) {
+	public static void java(LuanState luan) throws LuanException {
+		check(luan,luan.currentSource().name);
 		luan.currentEnvironment().setJava();
 	}
 
@@ -311,21 +312,6 @@
 
 	public static Static load(LuanState luan,String name) throws LuanException {
 		checkJava(luan);
-		@SuppressWarnings("unchecked")
-		Map<String,Static> loaded = (Map<String,Static>)luan.registry().get("Java.loaded");
-		if( loaded == null ) {
-			loaded = new HashMap<String,Static>();
-			luan.registry().put("Java.loaded",loaded);
-		}
-		Static s = loaded.get(name);
-		if( s == null ) {
-			s = getClassStatic(luan,name);
-			loaded.put(name,s);
-		}
-		return s;
-	}
-
-	private static Static getClassStatic(LuanState luan,String name) throws LuanException {
 		Class cls;
 		try {
 			cls = Class.forName(name);
@@ -433,4 +419,25 @@
 			}
 		);
 	}
+
+
+
+	// security
+
+	public interface Security {
+		public void check(LuanState luan,String name) throws LuanException;
+	}
+
+	private static String SECURITY_KEY = "Java.Security";
+
+	private static void check(LuanState luan,String name) throws LuanException {
+		Security s = (Security)luan.registry().get(SECURITY_KEY);
+		if( s!=null )
+			s.check(luan,name);
+	}
+
+	public static void setSecurity(LuanState luan,Security s) {
+		luan.registry().put(SECURITY_KEY,s);
+	}
+
 }
--- a/core/src/luan/modules/PackageLuan.java	Tue Dec 16 13:13:33 2014 +0000
+++ b/core/src/luan/modules/PackageLuan.java	Wed Dec 17 12:35:57 2014 +0000
@@ -41,16 +41,20 @@
 	}
 
 	public static Object load(LuanState luan,String modName) throws LuanException {
-		if( modName.startsWith("java:") )
-			return JavaLuan.load(luan,modName.substring(5));
 		LuanTable loaded = loaded(luan);
 		Object mod = loaded.get(modName);
 		if( mod == null ) {
-			String src = read(luan,modName+".luan");
-			if( src == null )
-				return null;
-			LuanFunction loader = BasicLuan.load(luan,src,modName,null,false);
-			mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",new Object[]{modName}));
+			if( modName.startsWith("java:") ) {
+				mod = JavaLuan.load(luan,modName.substring(5));
+			} else {
+				String src = read(luan,modName+".luan");
+				if( src == null )
+					return null;
+				LuanFunction loader = BasicLuan.load(luan,src,modName,null,false);
+				mod = Luan.first(
+					luan.call(loader,"<require \""+modName+"\">",new Object[]{modName})
+				);
+			}
 			if( mod != null ) {
 				loaded.put(modName,mod);
 			} else {
@@ -68,6 +72,10 @@
 		LuanTable t = IoLuan.Uri(luan,uri);
 		if( t == null )
 			return null;
+		LuanFunction existsFn = (LuanFunction)t.get("exists");
+		boolean exists = (Boolean)Luan.first(luan.call(existsFn));
+		if( !exists )
+			return null;
 		LuanFunction reader = (LuanFunction)t.get("read_text");
 		return (String)Luan.first(luan.call(reader));
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/modules/Thread.luan	Wed Dec 17 12:35:57 2014 +0000
@@ -0,0 +1,4 @@
+java()
+import "java:luan.modules.ThreadLuan"
+
+fork = ThreadLuan.fork
--- a/core/src/luan/modules/ThreadLuan.java	Tue Dec 16 13:13:33 2014 +0000
+++ b/core/src/luan/modules/ThreadLuan.java	Wed Dec 17 12:35:57 2014 +0000
@@ -6,29 +6,11 @@
 import luan.LuanState;
 import luan.LuanFunction;
 import luan.LuanTable;
-import luan.LuanJavaFunction;
 import luan.LuanException;
 import luan.DeepCloner;
 
 
 public final class ThreadLuan {
-
-	public static final LuanFunction LOADER = new LuanFunction() {
-		@Override public Object call(LuanState luan,Object[] args) {
-			LuanTable module = Luan.newTable();
-			try {
-				add( module, "fork", LuanState.class, LuanFunction.class, new Object[0].getClass() );
-			} catch(NoSuchMethodException e) {
-				throw new RuntimeException(e);
-			}
-			return module;
-		}
-	};
-
-	private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
-		t.put( method, new LuanJavaFunction(ThreadLuan.class.getMethod(method,parameterTypes),null) );
-	}
-
 	private static final Executor exec = Executors.newCachedThreadPool();
 
 	public static void fork(LuanState luan,LuanFunction fn,Object... args) {
@@ -44,5 +26,4 @@
 			}
 		}});
 	}
-
 }