Mercurial Hosting > luan
view src/luan/lib/PackageLib.java @ 141:c2ee8a717b73
move loading of standard modules to luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@142 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 13 Jun 2014 16:37:49 +0000 |
parents | 06159094b802 |
children | 05f8c21160ef |
line wrap: on
line source
package luan.lib; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.Arrays; import java.util.Collections; import java.util.List; import luan.Luan; import luan.LuanState; import luan.LuanTable; import luan.LuanFunction; import luan.LuanJavaFunction; import luan.LuanElement; import luan.LuanException; public final class PackageLib { public static final String NAME = "Package"; public static final LuanFunction LOADER = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) { LuanTable module = new LuanTable(); LuanTable global = luan.global(); module.put("loaded",luan.loaded()); module.put("preload",luan.preload()); module.put("path","?.luan"); try { add( global, "require", LuanState.class, String.class ); add( module, "get_loader", String.class ); add( module, "search_path", String.class, String.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } LuanTable searchers = luan.searchers(); searchers.add(preloadSearcher); searchers.add(fileSearcher); searchers.put("java",javaFileSearcher); module.put("searchers",searchers); return module; } }; private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { t.put( method, new LuanJavaFunction(PackageLib.class.getMethod(method,parameterTypes),null) ); } public static Object require(LuanState luan,String modName) throws LuanException { 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); } else { int i = modName.indexOf(':'); if( i != -1 ) { String prefix = modName.substring(0,i); Object searcher = searchers.get(prefix); if( searcher != null ) { list = Collections.<Object>singletonList(searcher); searchFor = modName.substring(i+1); } } if( list == null ) list = searchers.asList(); } 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 String search_path(String name,String path) { for( String s : path.split(";") ) { String file = s.replaceAll("\\?",name); if( new File(file).exists() ) return file; } return null; } public static final LuanFunction fileLoader = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) throws LuanException { String fileName = (String)args[1]; LuanFunction fn = BasicLib.load_file(luan,fileName); return fn.call(luan,args); } }; public static final LuanFunction fileSearcher = new LuanFunction() { @Override public Object[] call(LuanState luan,Object[] args) { String modName = (String)args[0]; String path = (String)luan.get("Package.path"); if( path==null ) return LuanFunction.NOTHING; String file = search_path(modName,path); return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file}; } }; 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 final LuanFunction javaFileLoader = new LuanFunction() { @Override public Object call(LuanState luan,Object[] args) throws LuanException { String urlStr = (String)args[1]; String path = (String)args[2]; try { String src = new IoLib.LuanUrl(urlStr).read_text(); LuanFunction fn = BasicLib.load(luan,src,path,false,false); return fn.call(luan,args); } catch(IOException e) { throw luan.exception(e); } } }; public static final LuanFunction javaFileSearcher = new LuanFunction() { @Override public Object[] call(LuanState luan,Object[] args) { String path = (String)args[0]; String url = IoLib.java_resource_to_url(path); if( url != null ) { return new Object[]{javaFileLoader,url,path}; } return LuanFunction.NOTHING; } }; public static LuanFunction get_loader(String path) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException { int i = path.lastIndexOf('.'); String clsPath = path.substring(0,i); String fld = path.substring(i+1); Class cls = Class.forName(clsPath); return (LuanFunction)cls.getField(fld).get(null); } }