comparison src/luan/modules/PackageLuan.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents 25746915a241
children 8d95711f6615
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
13 import luan.LuanException; 13 import luan.LuanException;
14 14
15 15
16 public final class PackageLuan { 16 public final class PackageLuan {
17 17
18 public static final LuanFunction requireFn; 18 public static LuanFunction requireFn(Luan luan) {
19 static { 19 LuanFunction fn = (LuanFunction)luan.registry().get("Package.require");
20 try { 20 if( fn == null ) {
21 requireFn = new LuanJavaFunction(PackageLuan.class.getMethod("require",Luan.class,String.class),null); 21 try {
22 } catch(NoSuchMethodException e) { 22 fn = new LuanJavaFunction(luan,PackageLuan.class.getMethod("require",Luan.class,String.class),null);
23 throw new RuntimeException(e); 23 } catch(NoSuchMethodException e) {
24 throw new RuntimeException(e);
25 }
26 luan.registry().put("Package.require",fn);
24 } 27 }
28 return fn;
25 } 29 }
26 30
27 public static LuanTable loaded(Luan luan) { 31 public static LuanTable loaded(Luan luan) {
28 LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded"); 32 LuanTable tbl = (LuanTable)luan.registry().get("Package.loaded");
29 if( tbl == null ) { 33 if( tbl == null ) {
51 src = Utils.readAll(in); 55 src = Utils.readAll(in);
52 in.close(); 56 in.close();
53 } catch(IOException e) { 57 } catch(IOException e) {
54 throw new RuntimeException(e); 58 throw new RuntimeException(e);
55 } 59 }
56 LuanFunction loader = Luan.load(src,modName); 60 LuanFunction loader = luan.load(src,modName);
57 mod = Luan.first( 61 mod = Luan.first(
58 loader.call(luan,new Object[]{modName}) 62 loader.call(modName)
59 ); 63 );
60 if( mod == null ) 64 if( mod == null )
61 throw new RuntimeException(); 65 throw new RuntimeException();
62 } else if( modName.startsWith("java:") ) { 66 } else if( modName.startsWith("java:") ) {
63 mod = JavaLuan.load(luan,modName.substring(5)); 67 mod = JavaLuan.load(luan,modName.substring(5));
66 } else { 70 } else {
67 String src = read(luan,modName); 71 String src = read(luan,modName);
68 if( src == null ) { 72 if( src == null ) {
69 mod = Boolean.FALSE; 73 mod = Boolean.FALSE;
70 } else { 74 } else {
71 LuanFunction loader = Luan.load(src,modName); 75 LuanFunction loader = luan.load(src,modName);
72 mod = Luan.first( 76 mod = Luan.first(
73 loader.call(luan,new Object[]{modName}) 77 loader.call(modName)
74 ); 78 );
75 if( mod == null ) { 79 if( mod == null ) {
76 mod = loaded.rawGet(modName); 80 mod = loaded.rawGet(modName);
77 if( mod != null ) 81 if( mod != null )
78 return mod; 82 return mod;
92 } catch(LuanException e) { 96 } catch(LuanException e) {
93 throw new RuntimeException(e); 97 throw new RuntimeException(e);
94 } 98 }
95 Luan.Security security = Luan.setSecurity(luan,null); 99 Luan.Security security = Luan.setSecurity(luan,null);
96 try { 100 try {
97 return (String)Luan.first(boot.call("read",uri)); 101 return (String)Luan.first(boot.fn("read").call(uri));
98 } catch(LuanException e) { 102 } catch(LuanException e) {
99 return null; 103 return null;
100 } finally { 104 } finally {
101 if( security != null ) 105 if( security != null )
102 Luan.setSecurity(luan,security); 106 Luan.setSecurity(luan,security);
103 } 107 }
104 } 108 }
105 109
106 public static void enableLoad(Luan luan,String... mods) throws LuanException {
107 if( !luan.isLocked )
108 return;
109 LuanTable loaded = loaded(luan);
110 for( String mod : mods ) {
111 if( loaded.rawGet(mod) == null ) {
112 luan.isLocked = false;
113 luan.deepenClone(luan,new LuanCloner(LuanCloner.Type.COMPLETE));
114 break;
115 }
116 }
117 }
118
119 } 110 }