comparison src/luan/modules/PackageLuan.java @ 1580:2975c932864d

require options
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 10 Feb 2021 23:56:59 -0700
parents c922446f53aa
children a37ffe2d1b14
comparison
equal deleted inserted replaced
1579:dd881eb03d87 1580:2975c932864d
19 19
20 public static LuanFunction requireFn(Luan luan) { 20 public static LuanFunction requireFn(Luan luan) {
21 LuanFunction fn = (LuanFunction)luan.registry().get("Package.require"); 21 LuanFunction fn = (LuanFunction)luan.registry().get("Package.require");
22 if( fn == null ) { 22 if( fn == null ) {
23 try { 23 try {
24 fn = new LuanJavaFunction(PackageLuan.class.getMethod("require",Luan.class,String.class),null); 24 fn = new LuanJavaFunction(PackageLuan.class.getMethod("require",Luan.class,String.class,LuanTable.class),null);
25 } catch(NoSuchMethodException e) { 25 } catch(NoSuchMethodException e) {
26 throw new RuntimeException(e); 26 throw new RuntimeException(e);
27 } 27 }
28 luan.registry().put("Package.require",fn); 28 luan.registry().put("Package.require",fn);
29 } 29 }
37 luan.registry().put("Package.loaded",map); 37 luan.registry().put("Package.loaded",map);
38 } 38 }
39 return map; 39 return map;
40 } 40 }
41 41
42 public static Object require(Luan luan,String modName) throws LuanException { 42 public static Object require(Luan luan,String modName,LuanTable options) throws LuanException {
43 if( "java".equals(modName) ) { 43 if( "java".equals(modName) ) {
44 JavaLuan.java(luan); 44 JavaLuan.java(luan);
45 return true; 45 return true;
46 } 46 }
47 Object mod = load(luan,modName); 47 Object mod = load(luan,modName,options);
48 if( mod.equals(Boolean.FALSE) ) 48 if( mod.equals(Boolean.FALSE) )
49 throw new LuanException( "module '"+modName+"' not found" ); 49 throw new LuanException( "module '"+modName+"' not found" );
50 return mod; 50 return mod;
51 } 51 }
52 52
53 public static Object load(Luan luan,String modName) throws LuanException { 53 public static Object load(Luan luan,String modName,LuanTable options) throws LuanException {
54 Map loaded = loaded(luan); 54 Map loaded = loaded(luan);
55 Object mod = loaded.get(modName); 55 Object mod = loaded.get(modName);
56 if( mod == null ) { 56 if( mod == null ) {
57 if( modName.equals("luan:Boot.luan") ) { 57 if( modName.equals("luan:Boot.luan") ) {
58 String src; 58 String src;
72 } else if( modName.startsWith("java:") ) { 72 } else if( modName.startsWith("java:") ) {
73 mod = JavaLuan.load(luan,modName.substring(5)); 73 mod = JavaLuan.load(luan,modName.substring(5));
74 if( mod == null ) 74 if( mod == null )
75 mod = Boolean.FALSE; 75 mod = Boolean.FALSE;
76 } else { 76 } else {
77 String src = read(luan,modName); 77 String src = read(luan,modName,options);
78 if( src == null ) { 78 if( src == null ) {
79 mod = Boolean.FALSE; 79 mod = Boolean.FALSE;
80 } else { 80 } else {
81 LuanFunction loader = luan.load(src,modName,true); 81 LuanFunction loader = luan.load(src,modName,true);
82 mod = Luan.first( 82 mod = Luan.first(
93 loaded.put(modName,mod); 93 loaded.put(modName,mod);
94 } 94 }
95 return mod; 95 return mod;
96 } 96 }
97 97
98 static String read(Luan luan,String uri) { 98 static String read(Luan luan,String uri,LuanTable options) {
99 LuanTable boot; 99 LuanTable boot;
100 try { 100 try {
101 boot = (LuanTable)luan.require("luan:Boot.luan"); 101 boot = (LuanTable)luan.require("luan:Boot.luan");
102 } catch(LuanException e) { 102 } catch(LuanException e) {
103 throw new RuntimeException(e); 103 throw new RuntimeException(e);
104 } 104 }
105 Luan.Security security = Luan.setSecurity(luan,null); 105 Luan.Security security = Luan.setSecurity(luan,null);
106 try { 106 try {
107 return (String)Luan.first(boot.fn("read").call(luan,uri)); 107 return (String)Luan.first(boot.fn("read").call(luan,uri,options));
108 } catch(LuanException e) { 108 } catch(LuanException e) {
109 return null; 109 return null;
110 } finally { 110 } finally {
111 if( security != null ) 111 if( security != null )
112 Luan.setSecurity(luan,security); 112 Luan.setSecurity(luan,security);