changeset 300:9fb523472035

add java() to control java access git-svn-id: https://luan-java.googlecode.com/svn/trunk@301 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 16 Dec 2014 13:13:33 +0000
parents a74559240b4f
children a6bf8ff720f8
files core/src/luan/AbstractLuanTable.java core/src/luan/LuanState.java core/src/luan/LuanTable.java core/src/luan/LuanTableImpl.java core/src/luan/cmd_line.luan core/src/luan/modules/Basic.luan core/src/luan/modules/Debug.luan core/src/luan/modules/Io.luan core/src/luan/modules/JavaLuan.java 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/Time.luan logging/src/luan/modules/logging/Logging.luan lucene/src/luan/modules/lucene/Lucene.luan mail/src/luan/modules/mail/Mail.luan web/src/luan/modules/web/Http.luan web/src/luan/modules/web/Web_server.luan web/src/luan/modules/web/web_shell.luan
diffstat 20 files changed, 61 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
diff -r a74559240b4f -r 9fb523472035 core/src/luan/AbstractLuanTable.java
--- a/core/src/luan/AbstractLuanTable.java	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/AbstractLuanTable.java	Tue Dec 16 13:13:33 2014 +0000
@@ -88,4 +88,12 @@
 	@Override public LuanTable cloneTable() {
 		return isList() ? new LuanTableImpl(new ArrayList<Object>(asList())) : new LuanTableImpl(newMap(asMap()));
 	}
+
+	@Override public boolean hasJava() {
+		throw new UnsupportedOperationException();
+	}
+
+	@Override public void setJava() {
+		throw new UnsupportedOperationException();
+	}
 }
diff -r a74559240b4f -r 9fb523472035 core/src/luan/LuanState.java
--- a/core/src/luan/LuanState.java	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/LuanState.java	Tue Dec 16 13:13:33 2014 +0000
@@ -27,6 +27,7 @@
 		registry = new LuanTableImpl();
 		global = new LuanTableImpl();
 		global.put("_G",global);
+		global.put("java",JavaLuan.javaFn);
 		metatable = newMetatable();
 	}
 
@@ -52,6 +53,7 @@
 		try {
 			LuanState luan = LuanCompiler.newLuanState();
 			PackageLuan.require(luan,"luan:Basic");
+			PackageLuan.require(luan,"luan:Io");
 //			BasicLuan.do_file(luan,"classpath:luan/init.luan");
 			return luan;
 		} catch(LuanException e) {
diff -r a74559240b4f -r 9fb523472035 core/src/luan/LuanTable.java
--- a/core/src/luan/LuanTable.java	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/LuanTable.java	Tue Dec 16 13:13:33 2014 +0000
@@ -21,4 +21,6 @@
 	public LuanTable getMetatable();
 	public void setMetatable(LuanTable metatable);
 	public LuanTable cloneTable();
+	public boolean hasJava();
+	public void setJava();
 }
diff -r a74559240b4f -r 9fb523472035 core/src/luan/LuanTableImpl.java
--- a/core/src/luan/LuanTableImpl.java	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/LuanTableImpl.java	Tue Dec 16 13:13:33 2014 +0000
@@ -18,6 +18,7 @@
 	private Map<Object,Object> map = null;
 	private List<Object> list = null;
 	private LuanTable metatable = null;
+	private boolean hasJava = false;
 
 	public LuanTableImpl() {}
 /*
@@ -76,6 +77,7 @@
 		}
 		if( metatable != null )
 			clone.metatable = cloner.get(metatable);
+		clone.hasJava = hasJava;
 	}
 
 	@Override public boolean isList() {
@@ -353,4 +355,12 @@
 	@Override public void setMetatable(LuanTable metatable) {
 		this.metatable = metatable;
 	}
+
+	@Override public boolean hasJava() {
+		return hasJava;
+	}
+
+	@Override public void setJava() {
+		hasJava = true;
+	}
 }
diff -r a74559240b4f -r 9fb523472035 core/src/luan/cmd_line.luan
--- a/core/src/luan/cmd_line.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/cmd_line.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,6 +1,7 @@
 import "luan:String"
 import "luan:Table"
 import "luan:Io"
+import "luan:Debug"
 
 
 local standalone_usage = [=[
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/Basic.luan
--- a/core/src/luan/modules/Basic.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/Basic.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "java:luan.modules.BasicLuan"
 
 assert = BasicLuan.assert_
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/Debug.luan
--- a/core/src/luan/modules/Debug.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/Debug.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,4 +1,5 @@
 import "luan:Table"
+import "luan:Io"
 
 
 function print_if_something(...)
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/Io.luan
--- a/core/src/luan/modules/Io.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/Io.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "java:luan.modules.IoLuan"
 import "java:java.lang.System"
 
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/JavaLuan.java
--- a/core/src/luan/modules/JavaLuan.java	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/JavaLuan.java	Tue Dec 16 13:13:33 2014 +0000
@@ -27,12 +27,32 @@
 
 public final class JavaLuan {
 
+	public static void java(LuanState luan) {
+		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");
+	}
+
 	private static boolean isLoaded(LuanState luan) {
 //		return PackageLuan.loaded(luan).get("luan:Java") != null;
 		return true;
 	}
 
 	public static Object __index(LuanState luan,Object obj,Object key) throws LuanException {
+		if( !luan.currentEnvironment().hasJava() )
+			return null;
 		if( obj instanceof Static ) {
 			if( key instanceof String ) {
 				String name = (String)key;
@@ -82,8 +102,6 @@
 			} else {
 				List<Member> members = getMembers(cls,name);
 				if( !members.isEmpty() ) {
-					if( name.equals("getClass") && !isLoaded(luan) )
-						return null;  // security
 					return member(obj,members);
 				}
 			}
@@ -121,6 +139,7 @@
 	}
 
 	public static void __newindex(LuanState luan,Object obj,Object key,Object value) throws LuanException {
+		checkJava(luan);
 		if( obj instanceof Static ) {
 			if( key instanceof String ) {
 				String name = (String)key;
@@ -291,6 +310,7 @@
 	}
 
 	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 ) {
@@ -318,12 +338,8 @@
 		}
 		return new Static(cls);
 	}
-/*
-	public static void importClass(LuanState luan,String name) throws LuanException {
-		luan.currentEnvironment().put( name.substring(name.lastIndexOf('.')+1), getClass(luan,name) );
-	}
-*/
-	static class AmbiguousJavaFunction extends LuanFunction {
+
+	private static class AmbiguousJavaFunction extends LuanFunction {
 		private final Map<Integer,List<LuanJavaFunction>> fnMap = new HashMap<Integer,List<LuanJavaFunction>>();
 
 		AmbiguousJavaFunction(List<LuanJavaFunction> fns) {
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/Math.luan
--- a/core/src/luan/modules/Math.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/Math.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "java:luan.modules.MathLuan"
 local JavaMath = require "java:java.lang.Math"
 
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/Package.luan
--- a/core/src/luan/modules/Package.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/Package.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "java:luan.modules.PackageLuan"
 
 loaded = PackageLuan.loaded()
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/String.luan
--- a/core/src/luan/modules/String.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/String.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "java:luan.modules.StringLuan"
 
 to_binary = StringLuan.to_binary
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/Table.luan
--- a/core/src/luan/modules/Table.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/Table.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "java:luan.modules.TableLuan"
 
 clone = TableLuan.clone
diff -r a74559240b4f -r 9fb523472035 core/src/luan/modules/Time.luan
--- a/core/src/luan/modules/Time.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/core/src/luan/modules/Time.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,5 +1,6 @@
 -- incomplete, will add as needed
 
+java()
 import "luan:String"
 import "luan:Table"
 import "java:java.lang.System"
diff -r a74559240b4f -r 9fb523472035 logging/src/luan/modules/logging/Logging.luan
--- a/logging/src/luan/modules/logging/Logging.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/logging/src/luan/modules/logging/Logging.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "java:org.apache.log4j.Logger"
 import "java:org.apache.log4j.EnhancedPatternLayout"
 import "java:org.apache.log4j.ConsoleAppender"
diff -r a74559240b4f -r 9fb523472035 lucene/src/luan/modules/lucene/Lucene.luan
--- a/lucene/src/luan/modules/lucene/Lucene.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/lucene/src/luan/modules/lucene/Lucene.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "luan:Table"
 import "java:luan.modules.lucene.LuceneIndex"
 import "java:org.apache.lucene.index.Term"
diff -r a74559240b4f -r 9fb523472035 mail/src/luan/modules/mail/Mail.luan
--- a/mail/src/luan/modules/mail/Mail.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/mail/src/luan/modules/mail/Mail.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "java:java.lang.System"
 import "java:luan.modules.mail.SmtpCon"
 
diff -r a74559240b4f -r 9fb523472035 web/src/luan/modules/web/Http.luan
--- a/web/src/luan/modules/web/Http.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/web/src/luan/modules/web/Http.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,3 +1,4 @@
+java()
 import "luan:Table"
 import "java:luan.modules.web.LuanHandler"
 
diff -r a74559240b4f -r 9fb523472035 web/src/luan/modules/web/Web_server.luan
--- a/web/src/luan/modules/web/Web_server.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/web/src/luan/modules/web/Web_server.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -4,6 +4,7 @@
 import "luan:web/Http"
 import "luan:logging/Logging"  -- initialize logging
 
+java()
 import "java:org.eclipse.jetty.server.Server"
 import "java:org.eclipse.jetty.server.NCSARequestLog"
 import "java:org.eclipse.jetty.server.handler.DefaultHandler"
diff -r a74559240b4f -r 9fb523472035 web/src/luan/modules/web/web_shell.luan
--- a/web/src/luan/modules/web/web_shell.luan	Tue Dec 16 06:24:49 2014 +0000
+++ b/web/src/luan/modules/web/web_shell.luan	Tue Dec 16 13:13:33 2014 +0000
@@ -1,5 +1,6 @@
 import "luan:Basic"
 import "luan:Io"
+import "luan:Debug"
 import "luan:web/Http"
 
 per_session = true