Mercurial Hosting > luan
view src/luan/modules/PackageLuan.java @ 1320:2c8d1d76a82a
PackageLuan.read() returns null for exceptions
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 04 Feb 2019 20:18:51 -0700 |
parents | 503bde9a7c80 |
children | f41919741100 |
line wrap: on
line source
package luan.modules; import java.io.Reader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import luan.Luan; import luan.LuanState; import luan.LuanTable; import luan.LuanFunction; import luan.LuanJavaFunction; import luan.LuanCloner; import luan.LuanException; public final class PackageLuan { public static final LuanFunction requireFn; static { try { requireFn = new LuanJavaFunction(PackageLuan.class.getMethod("require",LuanState.class,String.class),null); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } } public static LuanTable loaded(LuanState luan) { LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded"); if( tbl == null ) { tbl = new LuanTable(luan); luan.registry().put("Package.loaded",tbl); } return tbl; } public static Object require(LuanState luan,String modName) throws LuanException { Object mod = load(luan,modName); if( mod.equals(Boolean.FALSE) ) throw new LuanException( "module '"+modName+"' not found" ); return mod; } public static Object load(LuanState luan,String modName) throws LuanException { LuanTable loaded = loaded(luan); Object mod = loaded.rawGet(modName); if( mod == null ) { if( modName.equals("luan:Boot.luan") ) { String src; try { Reader in = new InputStreamReader(ClassLoader.getSystemResourceAsStream("luan/modules/Boot.luan")); src = Utils.readAll(in); in.close(); } catch(IOException e) { throw new RuntimeException(e); } LuanFunction loader = Luan.load(src,modName); mod = Luan.first( loader.call(luan,new Object[]{modName}) ); if( mod == null ) throw new RuntimeException(); } else if( modName.startsWith("java:") ) { mod = JavaLuan.load(luan,modName.substring(5)); if( mod == null ) mod = Boolean.FALSE; } else { String src = read(luan,modName); if( src == null ) { mod = Boolean.FALSE; } else { LuanFunction loader = Luan.load(src,modName); mod = Luan.first( loader.call(luan,new Object[]{modName}) ); if( mod == null ) { mod = loaded.rawGet(modName); if( mod != null ) return mod; throw new LuanException( "module '"+modName+"' returned nil" ); } } } loaded.rawPut(modName,mod); } return mod; } static String read(LuanState luan,String uri) { LuanTable boot; try { boot = (LuanTable)luan.require("luan:Boot.luan"); } catch(LuanException e) { throw new RuntimeException(e); } try { return (String)Luan.first(boot.call("read",uri)); } catch(LuanException e) { return null; } } public static void enableLoad(LuanState luan,String... mods) throws LuanException { if( !luan.isLocked ) return; LuanTable loaded = loaded(luan); for( String mod : mods ) { if( loaded.rawGet(mod) == null ) { luan.isLocked = false; luan.deepenClone(luan,new LuanCloner(LuanCloner.Type.COMPLETE)); break; } } } }