comparison core/src/luan/modules/PackageLuan.java @ 200:9fb218211763

add Package.block(); add LuanException.getFullMessage(); git-svn-id: https://luan-java.googlecode.com/svn/trunk@201 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 03 Jul 2014 22:22:16 +0000
parents be0275bda373
children 75750ceb45ee
comparison
equal deleted inserted replaced
199:8960c81eb4bc 200:9fb218211763
25 module.put("preload",luan.preload()); 25 module.put("preload",luan.preload());
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, "load", LuanState.class, String.class ); 31 add( module, "load", LuanState.class, String.class );
31 add( module, "load_lib", String.class ); 32 add( module, "load_lib", LuanState.class, String.class );
32 add( module, "search_path", String.class, String.class ); 33 add( module, "search_path", String.class, String.class );
33 add( module, "search", LuanState.class, String.class ); 34 add( module, "search", LuanState.class, String.class );
34 } catch(NoSuchMethodException e) { 35 } catch(NoSuchMethodException e) {
35 throw new RuntimeException(e); 36 throw new RuntimeException(e);
36 } 37 }
147 148
148 public static final LuanFunction javaLoader = new LuanFunction() { 149 public static final LuanFunction javaLoader = new LuanFunction() {
149 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 150 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
150 try { 151 try {
151 String objName = (String)args[1]; 152 String objName = (String)args[1];
152 LuanFunction fn = load_lib(objName); 153 LuanFunction fn = load_lib(luan,objName);
153 return fn.call(luan,args); 154 return fn.call(luan,args);
154 } catch(ClassNotFoundException e) { 155 } catch(ClassNotFoundException e) {
155 throw new RuntimeException(e); 156 throw new RuntimeException(e);
156 } catch(NoSuchFieldException e) { 157 } catch(NoSuchFieldException e) {
157 throw new RuntimeException(e); 158 throw new RuntimeException(e);
160 } 161 }
161 } 162 }
162 }; 163 };
163 164
164 public static final LuanFunction javaSearcher = new LuanFunction() { 165 public static final LuanFunction javaSearcher = new LuanFunction() {
165 @Override public Object[] call(LuanState luan,Object[] args) { 166 @Override public Object[] call(LuanState luan,Object[] args)
167 throws LuanException
168 {
166 String modName = (String)args[0]; 169 String modName = (String)args[0];
167 modName = modName.replace('/','.'); 170 modName = modName.replace('/','.');
168 String path = (String)luan.get("Package.jpath"); 171 String path = (String)luan.get("Package.jpath");
169 if( path==null ) 172 if( path==null )
170 path = jpath; 173 path = jpath;
171 for( String s : path.split(";") ) { 174 for( String s : path.split(";") ) {
172 String objName = s.replaceAll("\\?",modName); 175 String objName = s.replaceAll("\\?",modName);
173 try { 176 try {
174 load_lib(objName); // throws exception if not found 177 load_lib(luan,objName); // throws exception if not found
175 return new Object[]{javaLoader,objName}; 178 return new Object[]{javaLoader,objName};
176 } catch(ClassNotFoundException e) { 179 } catch(ClassNotFoundException e) {
177 } catch(NoSuchFieldException e) { 180 } catch(NoSuchFieldException e) {
178 } catch(IllegalAccessException e) { 181 } catch(IllegalAccessException e) {
179 } 182 }
181 return LuanFunction.NOTHING; 184 return LuanFunction.NOTHING;
182 } 185 }
183 }; 186 };
184 187
185 188
186 public static LuanFunction load_lib(String path) 189 public static void block_lib(LuanState luan,String path) {
187 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException 190 luan.blocked.add(path);
191 }
192
193 public static LuanFunction load_lib(LuanState luan,String path)
194 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, LuanException
188 { 195 {
196 if( luan.blocked.contains(path) )
197 throw luan.exception(path+" is blocked");
189 int i = path.lastIndexOf('.'); 198 int i = path.lastIndexOf('.');
190 String clsPath = path.substring(0,i); 199 String clsPath = path.substring(0,i);
191 String fld = path.substring(i+1); 200 String fld = path.substring(i+1);
192 Class cls = Class.forName(clsPath); 201 Class cls = Class.forName(clsPath);
193 return (LuanFunction)cls.getField(fld).get(null); 202 return (LuanFunction)cls.getField(fld).get(null);