comparison core/src/luan/modules/PackageLuan.java @ 202:75750ceb45ee

add LuanState.registry git-svn-id: https://luan-java.googlecode.com/svn/trunk@203 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 04 Jul 2014 17:18:39 +0000
parents 9fb218211763
children 5aafb5b9f70f
comparison
equal deleted inserted replaced
201:27abb3746917 202:75750ceb45ee
19 private static final String jpath = "luan.modules.?Luan.LOADER"; 19 private static final String jpath = "luan.modules.?Luan.LOADER";
20 20
21 public static final LuanFunction LOADER = new LuanFunction() { 21 public static final LuanFunction LOADER = new LuanFunction() {
22 @Override public Object call(LuanState luan,Object[] args) { 22 @Override public Object call(LuanState luan,Object[] args) {
23 LuanTable module = new LuanTable(); 23 LuanTable module = new LuanTable();
24 module.put("loaded",luan.loaded()); 24 module.put( "loaded", loaded(luan) );
25 module.put("preload",luan.preload()); 25 module.put( "preload", new LuanTable() );
26 module.put("path","?.luan;java:luan/modules/?.luan"); 26 module.put( "path", "?.luan;java:luan/modules/?.luan" );
27 module.put("jpath",jpath); 27 module.put( "jpath", jpath );
28 try { 28 try {
29 module.put("require",requireFn); 29 module.put("require",requireFn);
30 add( module, "block_lib", LuanState.class, String.class ); 30 add( module, "block_lib", LuanState.class, String.class );
31 add( module, "load", LuanState.class, String.class ); 31 add( module, "load", LuanState.class, String.class );
32 add( module, "load_lib", LuanState.class, String.class ); 32 add( module, "load_lib", LuanState.class, String.class );
33 add( module, "search_path", String.class, String.class ); 33 add( module, "search_path", String.class, String.class );
34 add( module, "search", LuanState.class, String.class ); 34 add( module, "search", LuanState.class, String.class );
35 } catch(NoSuchMethodException e) { 35 } catch(NoSuchMethodException e) {
36 throw new RuntimeException(e); 36 throw new RuntimeException(e);
37 } 37 }
38 LuanTable searchers = luan.searchers(); 38 module.put( "searchers", searchers(luan) );
39 searchers.add(preloadSearcher);
40 searchers.add(fileSearcher);
41 searchers.add(javaSearcher);
42 module.put("searchers",searchers);
43 return module; 39 return module;
44 } 40 }
45 }; 41 };
46 42
47 public static final LuanFunction requireFn; 43 public static final LuanFunction requireFn;
55 51
56 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 52 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
57 t.put( method, new LuanJavaFunction(PackageLuan.class.getMethod(method,parameterTypes),null) ); 53 t.put( method, new LuanJavaFunction(PackageLuan.class.getMethod(method,parameterTypes),null) );
58 } 54 }
59 55
56 public static LuanTable loaded(LuanState luan) {
57 return luan.registryTable("Package.loaded");
58 }
59
60 private static LuanTable blocked(LuanState luan) {
61 return luan.registryTable("Package.blocked");
62 }
63
64 private static Object pkg(LuanState luan,String key) {
65 LuanTable t = (LuanTable)loaded(luan).get("Package");
66 return t==null ? null : t.get(key);
67 }
68
69 private static LuanTable searchers(LuanState luan) {
70 String key = "Package.searchers";
71 LuanTable tbl = (LuanTable)luan.registry().get(key);
72 if( tbl == null ) {
73 tbl = new LuanTable();
74 tbl.add(preloadSearcher);
75 tbl.add(fileSearcher);
76 tbl.add(javaSearcher);
77 tbl.add(JavaLuan.javaSearcher);
78 luan.registry().put(key,tbl);
79 }
80 return tbl;
81 }
82
60 public static Object require(LuanState luan,String modName) throws LuanException { 83 public static Object require(LuanState luan,String modName) throws LuanException {
61 Object mod = load(luan,modName); 84 Object mod = load(luan,modName);
62 if( mod==null ) 85 if( mod==null )
63 throw luan.exception( "module '"+modName+"' not found" ); 86 throw luan.exception( "module '"+modName+"' not found" );
64 return mod; 87 return mod;
65 } 88 }
66 89
67 public static Object load(LuanState luan,String modName) throws LuanException { 90 public static Object load(LuanState luan,String modName) throws LuanException {
68 LuanTable loaded = luan.loaded(); 91 LuanTable loaded = loaded(luan);
69 Object mod = loaded.get(modName); 92 Object mod = loaded.get(modName);
70 if( mod == null ) { 93 if( mod == null ) {
71 Object[] a = search(luan,modName); 94 Object[] a = search(luan,modName);
72 if( a == null ) 95 if( a == null )
73 return null; 96 return null;
92 } 115 }
93 return mod; 116 return mod;
94 } 117 }
95 118
96 public static Object[] search(LuanState luan,String modName) throws LuanException { 119 public static Object[] search(LuanState luan,String modName) throws LuanException {
97 List<Object> list = null; 120 for( Object s : searchers(luan).asList() ) {
98 LuanTable searchers = (LuanTable)luan.get("Package.searchers");
99 if( searchers == null ) {
100 list = Collections.<Object>singletonList(javaSearcher);
101 } else {
102 list = searchers.asList();
103 }
104 for( Object s : list ) {
105 LuanFunction searcher = (LuanFunction)s; 121 LuanFunction searcher = (LuanFunction)s;
106 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName})); 122 Object[] a = Luan.array(luan.call(searcher,"<searcher>",new Object[]{modName}));
107 if( a.length >= 1 && a[0] instanceof LuanFunction ) 123 if( a.length >= 1 && a[0] instanceof LuanFunction )
108 return a; 124 return a;
109 } 125 }
111 } 127 }
112 128
113 public static final LuanFunction preloadSearcher = new LuanFunction() { 129 public static final LuanFunction preloadSearcher = new LuanFunction() {
114 @Override public Object call(LuanState luan,Object[] args) { 130 @Override public Object call(LuanState luan,Object[] args) {
115 String modName = (String)args[0]; 131 String modName = (String)args[0];
116 return luan.preload().get(modName); 132 LuanTable preload = (LuanTable)pkg(luan,"preload");
133 return preload==null ? LuanFunction.NOTHING : preload.get(modName);
117 } 134 }
118 }; 135 };
119 136
120 public static String search_path(String name,String path) { 137 public static String search_path(String name,String path) {
121 for( String s : path.split(";") ) { 138 for( String s : path.split(";") ) {
135 }; 152 };
136 153
137 public static final LuanFunction fileSearcher = new LuanFunction() { 154 public static final LuanFunction fileSearcher = new LuanFunction() {
138 @Override public Object[] call(LuanState luan,Object[] args) { 155 @Override public Object[] call(LuanState luan,Object[] args) {
139 String modName = (String)args[0]; 156 String modName = (String)args[0];
140 String path = (String)luan.get("Package.path"); 157 String path = (String)pkg(luan,"path");
141 if( path==null ) 158 if( path==null )
142 return LuanFunction.NOTHING; 159 return LuanFunction.NOTHING;
143 String file = search_path(modName,path); 160 String file = search_path(modName,path);
144 return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file}; 161 return file==null ? LuanFunction.NOTHING : new Object[]{fileLoader,file};
145 } 162 }
166 @Override public Object[] call(LuanState luan,Object[] args) 183 @Override public Object[] call(LuanState luan,Object[] args)
167 throws LuanException 184 throws LuanException
168 { 185 {
169 String modName = (String)args[0]; 186 String modName = (String)args[0];
170 modName = modName.replace('/','.'); 187 modName = modName.replace('/','.');
171 String path = (String)luan.get("Package.jpath"); 188 String path = (String)pkg(luan,"jpath");
172 if( path==null ) 189 if( path==null )
173 path = jpath; 190 path = jpath;
174 for( String s : path.split(";") ) { 191 for( String s : path.split(";") ) {
175 String objName = s.replaceAll("\\?",modName); 192 String objName = s.replaceAll("\\?",modName);
176 try { 193 try {
185 } 202 }
186 }; 203 };
187 204
188 205
189 public static void block_lib(LuanState luan,String path) { 206 public static void block_lib(LuanState luan,String path) {
190 luan.blocked.add(path); 207 blocked(luan).put(path,true);
191 } 208 }
192 209
193 public static LuanFunction load_lib(LuanState luan,String path) 210 public static LuanFunction load_lib(LuanState luan,String path)
194 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, LuanException 211 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, LuanException
195 { 212 {
196 if( luan.blocked.contains(path) ) 213 if( blocked(luan).get(path) != null )
197 throw luan.exception(path+" is blocked"); 214 throw luan.exception(path+" is blocked");
198 int i = path.lastIndexOf('.'); 215 int i = path.lastIndexOf('.');
199 String clsPath = path.substring(0,i); 216 String clsPath = path.substring(0,i);
200 String fld = path.substring(i+1); 217 String fld = path.substring(i+1);
201 Class cls = Class.forName(clsPath); 218 Class cls = Class.forName(clsPath);