Mercurial Hosting > luan
view src/luan/modules/PackageLuan.java @ 1842:22f73129eb4a default tip
add load_jar
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 01 Feb 2025 16:54:41 -0700 |
parents | a37ffe2d1b14 |
children |
line wrap: on
line source
package luan.modules; import java.io.Reader; import java.io.InputStreamReader; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.HashMap; import goodjava.io.IoUtils; import luan.Luan; import luan.LuanTable; import luan.LuanFunction; import luan.LuanJavaFunction; import luan.LuanException; public final class PackageLuan { public static LuanFunction requireFn(Luan luan) { LuanFunction fn = (LuanFunction)luan.registry().get("Package.require"); if( fn == null ) { try { fn = new LuanJavaFunction(PackageLuan.class.getMethod("require",Luan.class,String.class,LuanTable.class),null); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } luan.registry().put("Package.require",fn); } return fn; } public static Map loaded(Luan luan) { Map map = (Map)luan.registry().get("Package.loaded"); if( map == null ) { map = new HashMap(); luan.registry().put("Package.loaded",map); } return map; } public static Object require(Luan luan,String modName,LuanTable options) throws LuanException { if( "java".equals(modName) ) { JavaLuan.java(luan); return true; } Object mod = load(luan,modName,options); if( mod.equals(Boolean.FALSE) ) throw new LuanException( "module '"+modName+"' not found" ); return mod; } public static Object load(Luan luan,String modName,LuanTable options) throws LuanException { Map loaded = loaded(luan); Object mod = loaded.get(modName); if( mod == null ) { if( modName.equals("luan:Boot.luan") ) { String src; try { Reader in = new InputStreamReader(ClassLoader.getSystemResourceAsStream("luan/modules/Boot.luan")); src = IoUtils.readAll(in); in.close(); } catch(IOException e) { throw new RuntimeException(e); } LuanFunction loader = luan.load(src,modName,true); mod = Luan.first( loader.call(luan,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,options); if( src == null ) { mod = Boolean.FALSE; } else { LuanFunction loader = luan.load(src,modName,true); mod = Luan.first( loader.call(luan,modName) ); if( mod == null ) { mod = loaded.get(modName); if( mod != null ) return mod; throw new LuanException( "module '"+modName+"' returned nil" ); } } } loaded.put(modName,mod); } return mod; } static String read(Luan luan,String uri,LuanTable options) { LuanTable boot; try { boot = (LuanTable)luan.require("luan:Boot.luan"); } catch(LuanException e) { throw new RuntimeException(e); } Luan.Security security = Luan.setSecurity(luan,null); try { return (String)Luan.first(boot.fn(luan,"read").call(luan,uri,options)); } catch(LuanException e) { return null; } finally { if( security != null ) Luan.setSecurity(luan,security); } } public static void load_jar(Luan luan,Object jar) throws LuanException, IOException { Luan.checkSecurity(luan,"java"); File file = IoLuan.objToFile(luan,jar); if( file==null ) throw new LuanException( "bad argument #1 to 'load_jar' (string or file table expected)" ); luan.addJar(file); } }