Mercurial Hosting > luan
changeset 200:9fb218211763
add Package.block();
add LuanException.getFullMessage();
git-svn-id: https://luan-java.googlecode.com/svn/trunk@201 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 03 Jul 2014 22:22:16 +0000 |
parents | 8960c81eb4bc |
children | 27abb3746917 |
files | core/src/luan/Luan.java core/src/luan/LuanException.java core/src/luan/LuanState.java core/src/luan/impl/ExpressionsExpr.java core/src/luan/impl/LuanParser.java core/src/luan/impl/LuanStateImpl.java core/src/luan/init.luan core/src/luan/modules/PackageLuan.java |
diffstat | 8 files changed, 56 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/core/src/luan/Luan.java Thu Jul 03 21:37:47 2014 +0000 +++ b/core/src/luan/Luan.java Thu Jul 03 22:22:16 2014 +0000 @@ -11,7 +11,7 @@ LuanFunction standalone = (LuanFunction)BasicLuan.load_file(luan,"java:luan/cmd_line.luan"); luan.call(standalone,args); } catch(LuanException e) { - System.err.println(e.getMessage()); + System.err.println(e.getFullMessage()); // e.printStackTrace(); System.exit(-1); }
--- a/core/src/luan/LuanException.java Thu Jul 03 21:37:47 2014 +0000 +++ b/core/src/luan/LuanException.java Thu Jul 03 22:22:16 2014 +0000 @@ -1,7 +1,10 @@ package luan; +import java.io.StringWriter; +import java.io.PrintWriter; -public class LuanException extends Exception { + +public final class LuanException extends Exception { private final String stackTrace; LuanException(LuanBit bit,Object msg) { @@ -13,6 +16,18 @@ return super.getMessage() + stackTrace; } + public String getFullMessage() { + String msg = getMessage(); + Throwable cause = getCause(); + if( cause != null ) { + msg += "\nCaused by: "; + StringWriter sw = new StringWriter(); + cause.printStackTrace(new PrintWriter(sw)); + msg += sw; + } + return msg; + } + private String message() { return super.getMessage(); }
--- a/core/src/luan/LuanState.java Thu Jul 03 21:37:47 2014 +0000 +++ b/core/src/luan/LuanState.java Thu Jul 03 22:22:16 2014 +0000 @@ -6,6 +6,8 @@ import java.util.ArrayList; import java.util.Map; import java.util.LinkedHashMap; +import java.util.Set; +import java.util.HashSet; import luan.impl.LuanCompiler; import luan.modules.BasicLuan; import luan.modules.PackageLuan; @@ -19,6 +21,7 @@ private LuanTable loaded; private LuanTable preload; private LuanTable searchers; + public final Set<String> blocked; protected LuanState() { global = new LuanTable(); @@ -26,6 +29,11 @@ loaded = new LuanTable(); preload = new LuanTable(); searchers = new LuanTable(); + blocked = new HashSet<String>(); + } + + protected LuanState(LuanState luan) { + blocked = new HashSet<String>(luan.blocked); } @Override public void deepenClone(LuanState clone,DeepCloner cloner) {
--- a/core/src/luan/impl/ExpressionsExpr.java Thu Jul 03 21:37:47 2014 +0000 +++ b/core/src/luan/impl/ExpressionsExpr.java Thu Jul 03 22:22:16 2014 +0000 @@ -10,6 +10,8 @@ private final Expressions expressions; ExpressionsExpr(Expressions expressions) { + if( expressions==null ) + throw new NullPointerException(); this.expressions = expressions; }
--- a/core/src/luan/impl/LuanParser.java Thu Jul 03 21:37:47 2014 +0000 +++ b/core/src/luan/impl/LuanParser.java Thu Jul 03 22:22:16 2014 +0000 @@ -174,6 +174,8 @@ } private static Expr expr(Expressions exprs) { + if( exprs==null ) + return null; if( exprs instanceof Expr ) return (Expr)exprs; return new ExpressionsExpr(exprs);
--- a/core/src/luan/impl/LuanStateImpl.java Thu Jul 03 21:37:47 2014 +0000 +++ b/core/src/luan/impl/LuanStateImpl.java Thu Jul 03 22:22:16 2014 +0000 @@ -68,10 +68,14 @@ LuanStateImpl() {} + private LuanStateImpl(LuanStateImpl luan) { + super(luan); + } + @Override public LuanState shallowClone() { // if( frame != null ) // throw new IllegalStateException("frame isn't null"); - return new LuanStateImpl(); + return new LuanStateImpl(this); } // returns stack
--- a/core/src/luan/init.luan Thu Jul 03 21:37:47 2014 +0000 +++ b/core/src/luan/init.luan Thu Jul 03 22:22:16 2014 +0000 @@ -39,6 +39,13 @@ do_file "java:luan/version.luan" +function Package.block(mod_name) + local fn,path = Package.search(mod_name) + if path == nil then + error("module '"..mod_name.."' not found") + end + Package.block_lib(path) +end function Io.print_to(out,...) local list = {}
--- a/core/src/luan/modules/PackageLuan.java Thu Jul 03 21:37:47 2014 +0000 +++ b/core/src/luan/modules/PackageLuan.java Thu Jul 03 22:22:16 2014 +0000 @@ -27,8 +27,9 @@ module.put("jpath",jpath); try { module.put("require",requireFn); + add( module, "block_lib", LuanState.class, String.class ); add( module, "load", LuanState.class, String.class ); - add( module, "load_lib", String.class ); + add( module, "load_lib", LuanState.class, String.class ); add( module, "search_path", String.class, String.class ); add( module, "search", LuanState.class, String.class ); } catch(NoSuchMethodException e) { @@ -149,7 +150,7 @@ @Override public Object call(LuanState luan,Object[] args) throws LuanException { try { String objName = (String)args[1]; - LuanFunction fn = load_lib(objName); + LuanFunction fn = load_lib(luan,objName); return fn.call(luan,args); } catch(ClassNotFoundException e) { throw new RuntimeException(e); @@ -162,7 +163,9 @@ }; public static final LuanFunction javaSearcher = new LuanFunction() { - @Override public Object[] call(LuanState luan,Object[] args) { + @Override public Object[] call(LuanState luan,Object[] args) + throws LuanException + { String modName = (String)args[0]; modName = modName.replace('/','.'); String path = (String)luan.get("Package.jpath"); @@ -171,7 +174,7 @@ for( String s : path.split(";") ) { String objName = s.replaceAll("\\?",modName); try { - load_lib(objName); // throws exception if not found + load_lib(luan,objName); // throws exception if not found return new Object[]{javaLoader,objName}; } catch(ClassNotFoundException e) { } catch(NoSuchFieldException e) { @@ -183,9 +186,15 @@ }; - public static LuanFunction load_lib(String path) - throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException + public static void block_lib(LuanState luan,String path) { + luan.blocked.add(path); + } + + public static LuanFunction load_lib(LuanState luan,String path) + throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, LuanException { + if( luan.blocked.contains(path) ) + throw luan.exception(path+" is blocked"); int i = path.lastIndexOf('.'); String clsPath = path.substring(0,i); String fld = path.substring(i+1);