comparison core/src/luan/modules/IoLuan.java @ 264:9e0d4452e649

implement URL style module names git-svn-id: https://luan-java.googlecode.com/svn/trunk@265 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 29 Oct 2014 03:50:59 +0000
parents 54873a389f80
children 454a486d9c19
comparison
equal deleted inserted replaced
263:54873a389f80 264:9e0d4452e649
38 @Override public Object call(LuanState luan,Object[] args) { 38 @Override public Object call(LuanState luan,Object[] args) {
39 LuanTable module = Luan.newTable(); 39 LuanTable module = Luan.newTable();
40 try { 40 try {
41 add( module, "read_console_line", String.class ); 41 add( module, "read_console_line", String.class );
42 module.put( "protocols", newProtocols() ); 42 module.put( "protocols", newProtocols() );
43 add( module, "get", LuanState.class, String.class ); 43 add( module, "get", LuanState.class, String.class, Boolean.class );
44 module.put( "stdin", stdin.table() ); 44 module.put( "stdin", stdin.table() );
45 add( module, "socket_server", Integer.TYPE ); 45 add( module, "socket_server", Integer.TYPE );
46 } catch(NoSuchMethodException e) { 46 } catch(NoSuchMethodException e) {
47 throw new RuntimeException(e); 47 throw new RuntimeException(e);
48 } 48 }
204 public LuanFunction read_blocks(Integer blockSize) throws IOException { 204 public LuanFunction read_blocks(Integer blockSize) throws IOException {
205 int n = blockSize!=null ? blockSize : Utils.bufSize; 205 int n = blockSize!=null ? blockSize : Utils.bufSize;
206 return blocks(inputStream(),n); 206 return blocks(inputStream(),n);
207 } 207 }
208 208
209 public LuanFunction loader(LuanState luan,String name) throws LuanException {
210 try {
211 String src = read_text();
212 return BasicLuan.load(luan,src,name,null,false);
213 } catch(IOException e) {
214 throw luan.exception(e);
215 }
216 }
217
209 LuanTable table() { 218 LuanTable table() {
210 LuanTable tbl = Luan.newTable(); 219 LuanTable tbl = Luan.newTable();
211 try { 220 try {
212 tbl.put( "to_string", new LuanJavaFunction( 221 tbl.put( "to_string", new LuanJavaFunction(
213 LuanIn.class.getMethod( "to_string" ), this 222 LuanIn.class.getMethod( "to_string" ), this
222 LuanIn.class.getMethod( "read_lines" ), this 231 LuanIn.class.getMethod( "read_lines" ), this
223 ) ); 232 ) );
224 tbl.put( "read_blocks", new LuanJavaFunction( 233 tbl.put( "read_blocks", new LuanJavaFunction(
225 LuanIn.class.getMethod( "read_blocks", Integer.class ), this 234 LuanIn.class.getMethod( "read_blocks", Integer.class ), this
226 ) ); 235 ) );
236 tbl.put( "loader", new LuanJavaFunction(
237 LuanIn.class.getMethod( "loader", LuanState.class, String.class ), this
238 ) );
227 } catch(NoSuchMethodException e) { 239 } catch(NoSuchMethodException e) {
228 throw new RuntimeException(e); 240 throw new RuntimeException(e);
229 } 241 }
230 return tbl; 242 return tbl;
231 } 243 }
232 } 244 }
233 245
234 private static final LuanIn stdin = new LuanIn() { 246 static final LuanIn stdin = new LuanIn() {
235 247
236 @Override InputStream inputStream() { 248 @Override InputStream inputStream() {
237 return System.in; 249 return System.in;
238 } 250 }
239 251
395 } 407 }
396 return tbl; 408 return tbl;
397 } 409 }
398 } 410 }
399 411
400 public static LuanTable file(LuanState luan,String name) throws LuanException { 412 public static LuanTable file(LuanState luan,String name,Boolean loading) throws LuanException {
413 if( Boolean.TRUE.equals(loading) )
414 name += ".luan";
401 File file = new File(name); 415 File file = new File(name);
402 if( !file.exists() ) 416 if( !file.exists() )
403 return null; 417 return null;
404 return new LuanFile(file).table(); 418 return new LuanFile(file).table();
405 } 419 }
406 420
407 public static LuanTable classpath(LuanState luan,String path) throws LuanException { 421 public static LuanTable classpath(LuanState luan,String name,Boolean loading) throws LuanException {
408 if( path.contains("//") ) 422 if( name.contains("//") )
409 return null; 423 return null;
424 String path = name;
425 boolean isLoading = Boolean.TRUE.equals(loading);
426 if( isLoading )
427 path += ".luan";
410 URL url; 428 URL url;
411 if( !path.contains("#") ) { 429 if( !path.contains("#") ) {
412 url = ClassLoader.getSystemResource(path); 430 url = ClassLoader.getSystemResource(path);
413 } else { 431 } else {
414 String[] a = path.split("#"); 432 String[] a = path.split("#");
425 break; 443 break;
426 } 444 }
427 } 445 }
428 } 446 }
429 } 447 }
430 if( url == null ) 448 if( url != null )
449 return new LuanUrl(url).table();
450
451 // try java
452 if( !isLoading )
431 return null; 453 return null;
432 return new LuanUrl(url).table(); 454 String modName = name.replace('/','.') + "Luan.LOADER";
433 } 455 try {
434 456 //System.out.println("modName = "+modName);
435 private static LuanTable url(String url) throws IOException { 457 final LuanFunction fn = PackageLuan.load_lib(luan,modName); // throws exception if not found
458 LuanFunction loader = new LuanFunction() {
459 @Override public Object call(LuanState luan,Object[] args) {
460 return fn;
461 }
462 };
463 LuanTable tbl = Luan.newTable();
464 tbl.put( "loader", loader );
465 return tbl;
466 } catch(ClassNotFoundException e) {
467 } catch(NoSuchFieldException e) {
468 } catch(IllegalAccessException e) {
469 }
470 return null;
471 }
472
473 private static LuanTable url(String url,Boolean loading) throws IOException {
474 if( Boolean.TRUE.equals(loading) )
475 url += ".luan";
436 return new LuanUrl(new URL(url)).table(); 476 return new LuanUrl(new URL(url)).table();
437 } 477 }
438 478
439 public static LuanTable http(String path) throws IOException { 479 public static LuanTable http(String path,Boolean loading) throws IOException {
440 return url("http:"+path); 480 return url("http:"+path,loading);
441 } 481 }
442 482
443 public static LuanTable https(String path) throws IOException { 483 public static LuanTable https(String path,Boolean loading) throws IOException {
444 return url("https:"+path); 484 return url("https:"+path,loading);
485 }
486
487 public static LuanTable _class(LuanState luan,String path,Boolean loading) throws LuanException {
488 if( !Boolean.TRUE.equals(loading) )
489 return null;
490 final LuanFunction fn = JavaLuan.javaLoader(luan,path);
491 if( fn==null )
492 return null;
493 LuanFunction loader = new LuanFunction() {
494 @Override public Object call(LuanState luan,Object[] args) {
495 return fn;
496 }
497 };
498 LuanTable tbl = Luan.newTable();
499 tbl.put( "loader", loader );
500 return tbl;
501 }
502
503 public static LuanTable luan(LuanState luan,String path,Boolean loading) throws LuanException {
504 return classpath( luan, "luan/modules/" + path, loading );
445 } 505 }
446 506
447 private static LuanTable newProtocols() { 507 private static LuanTable newProtocols() {
448 LuanTable protocols = Luan.newTable(); 508 LuanTable protocols = Luan.newTable();
449 try { 509 try {
450 add( protocols, "file", LuanState.class, String.class ); 510 add( protocols, "file", LuanState.class, String.class, Boolean.class );
451 add( protocols, "classpath", LuanState.class, String.class ); 511 add( protocols, "classpath", LuanState.class, String.class, Boolean.class );
452 add( protocols, "socket", LuanState.class, String.class ); 512 add( protocols, "socket", LuanState.class, String.class );
453 add( protocols, "http", String.class ); 513 add( protocols, "http", String.class, Boolean.class );
454 add( protocols, "https", String.class ); 514 add( protocols, "https", String.class, Boolean.class );
515 protocols.put( "class", new LuanJavaFunction(
516 IoLuan.class.getMethod( "_class", LuanState.class, String.class, Boolean.class ), null
517 ) );
518 add( protocols, "luan", LuanState.class, String.class, Boolean.class );
455 } catch(NoSuchMethodException e) { 519 } catch(NoSuchMethodException e) {
456 throw new RuntimeException(e); 520 throw new RuntimeException(e);
457 } 521 }
458 return protocols; 522 return protocols;
459 } 523 }
460 524
461 private static LuanTable protocols(LuanState luan) { 525 private static LuanTable protocols(LuanState luan) {
462 LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("Io"); 526 LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("luan:Io");
463 if( t == null ) 527 if( t == null )
464 return newProtocols(); 528 return newProtocols();
465 t = (LuanTable)t.get("protocols"); 529 t = (LuanTable)t.get("protocols");
466 if( t == null ) 530 if( t == null )
467 return newProtocols(); 531 return newProtocols();
468 return t; 532 return t;
469 } 533 }
470 534
471 public static LuanTable get(LuanState luan,String name) throws LuanException { 535 public static LuanTable get(LuanState luan,String name,Boolean loading) throws LuanException {
472 int i = name.indexOf(':'); 536 int i = name.indexOf(':');
473 if( i == -1 ) 537 if( i == -1 )
474 throw luan.exception( "invalid IO name '"+name+"', missing protocol" ); 538 throw luan.exception( "invalid Io name '"+name+"', missing protocol" );
475 String protocol = name.substring(0,i); 539 String protocol = name.substring(0,i);
476 String location = name.substring(i+1); 540 String location = name.substring(i+1);
477 LuanTable protocols = protocols(luan); 541 LuanTable protocols = protocols(luan);
478 LuanFunction opener = (LuanFunction)protocols.get(protocol); 542 LuanFunction opener = (LuanFunction)protocols.get(protocol);
479 if( opener == null ) 543 if( opener == null )
480 throw luan.exception( "invalid protocol '"+protocol+"' in '"+name+"'" ); 544 throw luan.exception( "invalid protocol '"+protocol+"' in '"+name+"'" );
481 return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location})); 545 return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location,loading}));
482 } 546 }
483 547
484 public static final class LuanSocket extends LuanIO { 548 public static final class LuanSocket extends LuanIO {
485 private final Socket socket; 549 private final Socket socket;
486 550