Mercurial Hosting > luan
changeset 161:d310ebf4d6e7
add javaSearcher
git-svn-id: https://luan-java.googlecode.com/svn/trunk@162 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 19 Jun 2014 10:44:20 +0000 |
parents | 138b9baee80b |
children | 01e9707a2fb0 |
files | src/luan/LuanState.java src/luan/lib/OsLib.java src/luan/lib/PackageLib.java src/luan/lib/StringLib.java src/luan/lib/Utils.java src/luan/lib/init.luan |
diffstat | 6 files changed, 111 insertions(+), 56 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/LuanState.java Thu Jun 19 07:02:16 2014 +0000 +++ b/src/luan/LuanState.java Thu Jun 19 10:44:20 2014 +0000 @@ -131,18 +131,15 @@ return t.put(a[a.length-1],value); } - private final void load(String modName,LuanFunction loader) throws LuanException { - preload.put(modName,loader); + public final void globalImport(String modName) throws LuanException { Object mod = PackageLib.require(this,modName); - if( mod==null ) - throw new RuntimeException(); global.put(modName,mod); } public static LuanState newStandard() { try { LuanState luan = LuanCompiler.newLuanState(); - luan.load("Package",PackageLib.LOADER); + luan.globalImport("Package"); BasicLib.do_file(luan,"java:luan/lib/init.luan"); return luan; } catch(LuanException e) {
--- a/src/luan/lib/OsLib.java Thu Jun 19 07:02:16 2014 +0000 +++ b/src/luan/lib/OsLib.java Thu Jun 19 10:44:20 2014 +0000 @@ -52,6 +52,10 @@ return list; } + public boolean exists() { + return Utils.exists(file); + } + LuanTable table() { LuanTable tbl = new IoLib.LuanFile(file).table(); try { @@ -59,7 +63,7 @@ File.class.getMethod( "getName" ), file ) ); tbl.put( "exists", new LuanJavaFunction( - File.class.getMethod( "exists" ), file + LuanFile.class.getMethod( "exists" ), this ) ); tbl.put( "is_directory", new LuanJavaFunction( File.class.getMethod( "isDirectory" ), file
--- a/src/luan/lib/PackageLib.java Thu Jun 19 07:02:16 2014 +0000 +++ b/src/luan/lib/PackageLib.java Thu Jun 19 10:44:20 2014 +0000 @@ -15,6 +15,8 @@ public final class PackageLib { + private static final String jpath = "luan.lib.?Lib.LOADER"; + public static final LuanFunction LOADER = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) { LuanTable module = new LuanTable(); @@ -22,16 +24,19 @@ module.put("loaded",luan.loaded()); module.put("preload",luan.preload()); module.put("path","?.luan;java:luan/modules/?.luan"); + module.put("jpath",jpath); try { add( global, "require", LuanState.class, String.class ); - add( module, "get_loader", String.class ); + add( module, "load_lib", String.class ); add( module, "search_path", String.class, String.class ); + add( module, "search", LuanState.class, String.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } LuanTable searchers = luan.searchers(); searchers.add(preloadSearcher); searchers.add(fileSearcher); + searchers.add(javaSearcher); module.put("searchers",searchers); return module; } @@ -45,37 +50,47 @@ LuanTable loaded = luan.loaded(); Object mod = loaded.get(modName); if( mod == null ) { - List<Object> list = null; - String searchFor = modName; - LuanTable searchers = (LuanTable)luan.get("Package.searchers"); - if( searchers == null ) { - list = Collections.<Object>singletonList(preloadSearcher); + Object[] a = search(luan,modName); + if( a == null ) + throw luan.exception( "module '"+modName+"' not found" ); + LuanFunction loader = (LuanFunction)a[0]; + a[0] = modName; + mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a)); + if( mod != null ) { + loaded.put(modName,mod); } else { - list = searchers.asList(); + mod = loaded.get(modName); + if( mod==null ) + loaded.put(modName,true); } - for( Object s : list ) { - LuanFunction searcher = (LuanFunction)s; - Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{searchFor})); - if( a.length >= 1 && a[0] instanceof LuanFunction ) { - LuanFunction loader = (LuanFunction)a[0]; - a[0] = modName; - mod = Luan.first(luan.call(loader,"<require \""+modName+"\">",a)); - if( mod != null ) { - loaded.put(modName,mod); - } else { - mod = loaded.get(modName); - if( mod==null ) - loaded.put(modName,true); - } - break; - } - } - if( mod == null ) - throw luan.exception( "module '"+modName+"' not found" ); } return mod; } + public static Object[] search(LuanState luan,String modName) throws LuanException { + List<Object> list = null; + LuanTable searchers = (LuanTable)luan.get("Package.searchers"); + if( searchers == null ) { + list = Collections.<Object>singletonList(javaSearcher); + } else { + list = searchers.asList(); + } + for( Object s : list ) { + LuanFunction searcher = (LuanFunction)s; + Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName})); + if( a.length >= 1 && a[0] instanceof LuanFunction ) + return a; + } + return null; + } + + public static final LuanFunction preloadSearcher = new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) { + String modName = (String)args[0]; + return luan.preload().get(modName); + } + }; + public static String search_path(String name,String path) { for( String s : path.split(";") ) { String file = s.replaceAll("\\?",name); @@ -104,16 +119,45 @@ } }; - public static final LuanFunction preloadSearcher = new LuanFunction() { - @Override public Object call(LuanState luan,Object[] args) { + + public static final LuanFunction javaLoader = new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) throws LuanException { + try { + String objName = (String)args[1]; + LuanFunction fn = load_lib(objName); + return fn.call(luan,args); + } catch(ClassNotFoundException e) { + throw new RuntimeException(e); + } catch(NoSuchFieldException e) { + throw new RuntimeException(e); + } catch(IllegalAccessException e) { + throw new RuntimeException(e); + } + } + }; + + public static final LuanFunction javaSearcher = new LuanFunction() { + @Override public Object[] call(LuanState luan,Object[] args) { String modName = (String)args[0]; - return luan.preload().get(modName); + String path = (String)luan.get("Package.jpath"); + if( path==null ) + path = jpath; + for( String s : path.split(";") ) { + String objName = s.replaceAll("\\?",modName); + try { + load_lib(objName); // throws exception if not found + return new Object[]{javaLoader,objName}; + } catch(ClassNotFoundException e) { + } catch(NoSuchFieldException e) { + } catch(IllegalAccessException e) { + } + } + return LuanFunction.NOTHING; } }; - - public static LuanFunction get_loader(String path) + public static LuanFunction load_lib(String path) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { int i = path.lastIndexOf('.');
--- a/src/luan/lib/StringLib.java Thu Jun 19 07:02:16 2014 +0000 +++ b/src/luan/lib/StringLib.java Thu Jun 19 10:44:20 2014 +0000 @@ -171,7 +171,7 @@ return new String[]{m.group()}; String[] rtn = new String[n]; for( int i=0; i<n; i++ ) { - rtn[i] = m.group(i); + rtn[i] = m.group(i+1); } return rtn; }
--- a/src/luan/lib/Utils.java Thu Jun 19 07:02:16 2014 +0000 +++ b/src/luan/lib/Utils.java Thu Jun 19 10:44:20 2014 +0000 @@ -52,8 +52,16 @@ return out.toByteArray(); } + public static boolean exists(File file) { + try { + return file.exists() && file.getName().equals(file.getCanonicalFile().getName()); + } catch(IOException e) { + throw new RuntimeException(e); + } + } + public static boolean isFile(String path) { - return new File(path).exists(); + return exists(new File(path)); } public static String toUrl(String path) {
--- a/src/luan/lib/init.luan Thu Jun 19 07:02:16 2014 +0000 +++ b/src/luan/lib/init.luan Thu Jun 19 10:44:20 2014 +0000 @@ -1,25 +1,27 @@ -function Package.preload_java(name,path) - Package.preload[name] = Package.get_loader(path) -end - -function Package.load_java(name,path) - Package.preload_java(name,path) +function Package.global_import(name) local mod = require(name) _G[name] = mod return mod end -local Basic = Package.load_java("Basic","luan.lib.BasicLib.LOADER") -local String = Package.load_java("String","luan.lib.StringLib.LOADER") -local Table = Package.load_java("Table","luan.lib.TableLib.LOADER") -local Io = Package.load_java("Io","luan.lib.IoLib.LOADER") -Package.load_java("Math","luan.lib.MathLib.LOADER") -Package.load_java("Html","luan.lib.HtmlLib.LOADER") -Package.load_java("Thread","luan.lib.ThreadLib.LOADER") -Package.load_java("Binary","luan.lib.BinaryLib.LOADER") -Package.load_java("Os","luan.lib.OsLib.LOADER") -Package.preload_java("Java","luan.lib.JavaLib.LOADER") -Package.preload_java("Http","luan.lib.HttpLib.LOADER") +local Basic = Package.global_import("Basic","luan.lib.BasicLib.LOADER") +local String = Package.global_import("String","luan.lib.StringLib.LOADER") + +-- improved global_import +function Package.global_import(name) + local short = name.match("\.([^.]+)$") or name + local mod = require(name) + _G[short] = mod + return mod +end + +local Table = Package.global_import("Table","luan.lib.TableLib.LOADER") +local Io = Package.global_import("Io","luan.lib.IoLib.LOADER") +Package.global_import("Math","luan.lib.MathLib.LOADER") +Package.global_import("Html","luan.lib.HtmlLib.LOADER") +Package.global_import("Thread","luan.lib.ThreadLib.LOADER") +Package.global_import("Binary","luan.lib.BinaryLib.LOADER") +Package.global_import("Os","luan.lib.OsLib.LOADER") function Io.print_to(out,...)