comparison core/src/luan/modules/IoLuan.java @ 272:eb27e765affb

rename Io.protocols to Io.schemes and "class:" to "java:" git-svn-id: https://luan-java.googlecode.com/svn/trunk@273 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 31 Oct 2014 04:50:24 +0000
parents 1507149fc447
children 8ac3eaf8ecd9
comparison
equal deleted inserted replaced
271:82a3ebcfbafa 272:eb27e765affb
38 public static final LuanFunction LOADER = new LuanFunction() { 38 public static final LuanFunction LOADER = new LuanFunction() {
39 @Override public Object call(LuanState luan,Object[] args) { 39 @Override public Object call(LuanState luan,Object[] args) {
40 LuanTable module = Luan.newTable(); 40 LuanTable module = Luan.newTable();
41 try { 41 try {
42 add( module, "read_console_line", String.class ); 42 add( module, "read_console_line", String.class );
43 module.put( "protocols", newProtocols() ); 43 module.put( "schemes", newSchemes() );
44 add( module, "Uri", LuanState.class, String.class, Boolean.class ); 44 add( module, "Uri", LuanState.class, String.class, Boolean.class );
45 module.put( "stdin", stdin.table() ); 45 module.put( "stdin", stdin.table() );
46 add( module, "socket_server", Integer.TYPE ); 46 add( module, "socket_server", Integer.TYPE );
47 } catch(NoSuchMethodException e) { 47 } catch(NoSuchMethodException e) {
48 throw new RuntimeException(e); 48 throw new RuntimeException(e);
496 496
497 public static LuanTable https(String path,Boolean loading) throws IOException { 497 public static LuanTable https(String path,Boolean loading) throws IOException {
498 return url("https:"+path,loading); 498 return url("https:"+path,loading);
499 } 499 }
500 500
501 public static LuanTable _class(LuanState luan,String path,Boolean loading) throws LuanException { 501 public static LuanTable java(LuanState luan,String path,Boolean loading) throws LuanException {
502 if( !Boolean.TRUE.equals(loading) ) 502 if( !Boolean.TRUE.equals(loading) )
503 return null; 503 return null;
504 final LuanFunction fn = JavaLuan.javaLoader(luan,path); 504 final LuanFunction fn = JavaLuan.javaLoader(luan,path);
505 if( fn==null ) 505 if( fn==null )
506 return null; 506 return null;
521 public static LuanTable stdin(LuanState luan) throws LuanException { 521 public static LuanTable stdin(LuanState luan) throws LuanException {
522 LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io"); 522 LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io");
523 return (LuanTable)io.get("stdin"); 523 return (LuanTable)io.get("stdin");
524 } 524 }
525 525
526 private static LuanTable newProtocols() { 526 private static LuanTable newSchemes() {
527 LuanTable protocols = Luan.newTable(); 527 LuanTable schemes = Luan.newTable();
528 try { 528 try {
529 add( protocols, "file", LuanState.class, String.class, Boolean.class ); 529 add( schemes, "file", LuanState.class, String.class, Boolean.class );
530 add( protocols, "classpath", LuanState.class, String.class, Boolean.class ); 530 add( schemes, "classpath", LuanState.class, String.class, Boolean.class );
531 add( protocols, "socket", LuanState.class, String.class ); 531 add( schemes, "socket", LuanState.class, String.class );
532 add( protocols, "http", String.class, Boolean.class ); 532 add( schemes, "http", String.class, Boolean.class );
533 add( protocols, "https", String.class, Boolean.class ); 533 add( schemes, "https", String.class, Boolean.class );
534 protocols.put( "class", new LuanJavaFunction( 534 add( schemes, "java", LuanState.class, String.class, Boolean.class );
535 IoLuan.class.getMethod( "_class", LuanState.class, String.class, Boolean.class ), null 535 add( schemes, "luan", LuanState.class, String.class, Boolean.class );
536 ) ); 536 add( schemes, "stdin", LuanState.class );
537 add( protocols, "luan", LuanState.class, String.class, Boolean.class );
538 add( protocols, "stdin", LuanState.class );
539 } catch(NoSuchMethodException e) { 537 } catch(NoSuchMethodException e) {
540 throw new RuntimeException(e); 538 throw new RuntimeException(e);
541 } 539 }
542 return protocols; 540 return schemes;
543 } 541 }
544 542
545 private static LuanTable protocols(LuanState luan) { 543 private static LuanTable schemes(LuanState luan) {
546 LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("luan:Io"); 544 LuanTable t = (LuanTable)PackageLuan.loaded(luan).get("luan:Io");
547 if( t == null ) 545 if( t == null )
548 return newProtocols(); 546 return newSchemes();
549 t = (LuanTable)t.get("protocols"); 547 t = (LuanTable)t.get("schemes");
550 if( t == null ) 548 if( t == null )
551 return newProtocols(); 549 return newSchemes();
552 return t; 550 return t;
553 } 551 }
554 552
555 public static LuanTable Uri(LuanState luan,String name,Boolean loading) throws LuanException { 553 public static LuanTable Uri(LuanState luan,String name,Boolean loading) throws LuanException {
556 int i = name.indexOf(':'); 554 int i = name.indexOf(':');
557 if( i == -1 ) 555 if( i == -1 )
558 throw luan.exception( "invalid Io name '"+name+"', missing protocol" ); 556 throw luan.exception( "invalid Io name '"+name+"', missing scheme" );
559 String protocol = name.substring(0,i); 557 String scheme = name.substring(0,i);
560 String location = name.substring(i+1); 558 String location = name.substring(i+1);
561 LuanTable protocols = protocols(luan); 559 LuanTable schemes = schemes(luan);
562 LuanFunction opener = (LuanFunction)protocols.get(protocol); 560 LuanFunction opener = (LuanFunction)schemes.get(scheme);
563 if( opener == null ) 561 if( opener == null )
564 throw luan.exception( "invalid protocol '"+protocol+"' in '"+name+"'" ); 562 throw luan.exception( "invalid scheme '"+scheme+"' in '"+name+"'" );
565 return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location,loading})); 563 return (LuanTable)Luan.first(luan.call(opener,"<open \""+name+"\">",new Object[]{location,loading}));
566 } 564 }
567 565
568 public static final class LuanSocket extends LuanIO { 566 public static final class LuanSocket extends LuanIO {
569 private final Socket socket; 567 private final Socket socket;