comparison core/src/luan/modules/IoLuan.java @ 297:899253043270

remove PackageLuan.load_lib() git-svn-id: https://luan-java.googlecode.com/svn/trunk@298 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 16 Dec 2014 03:26:43 +0000
parents 7ea6dfdf81ba
children a74559240b4f
comparison
equal deleted inserted replaced
296:7ea6dfdf81ba 297:899253043270
33 import luan.LuanException; 33 import luan.LuanException;
34 34
35 35
36 public final class IoLuan { 36 public final class IoLuan {
37 37
38 public static final LuanFunction LOADER = new LuanFunction() {
39 @Override public Object call(LuanState luan,Object[] args) {
40 LuanTable module = Luan.newTable();
41 try {
42 add( module, "read_console_line", String.class );
43 module.put( "schemes", newSchemes() );
44 add( module, "Uri", LuanState.class, String.class, Boolean.class );
45 module.put( "stdin", stdin.table() );
46 add( module, "socket_server", Integer.TYPE );
47 } catch(NoSuchMethodException e) {
48 throw new RuntimeException(e);
49 }
50 module.put( "stdout", textWriter(System.out) );
51 module.put( "stderr", textWriter(System.err) );
52 return module;
53 }
54 };
55
56 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 38 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
57 t.put( method, new LuanJavaFunction(IoLuan.class.getMethod(method,parameterTypes),null) ); 39 t.put( method, new LuanJavaFunction(IoLuan.class.getMethod(method,parameterTypes),null) );
58 } 40 }
59
60 41
61 public static String read_console_line(String prompt) throws IOException { 42 public static String read_console_line(String prompt) throws IOException {
62 if( prompt==null ) 43 if( prompt==null )
63 prompt = "> "; 44 prompt = "> ";
64 return System.console().readLine(prompt); 45 return System.console().readLine(prompt);
225 } catch(FileNotFoundException e) { 206 } catch(FileNotFoundException e) {
226 return false; 207 return false;
227 } 208 }
228 } 209 }
229 210
230 LuanTable table() { 211 public LuanTable table() {
231 LuanTable tbl = Luan.newTable(); 212 LuanTable tbl = Luan.newTable();
232 try { 213 try {
233 tbl.put( "to_string", new LuanJavaFunction( 214 tbl.put( "to_string", new LuanJavaFunction(
234 LuanIn.class.getMethod( "to_string" ), this 215 LuanIn.class.getMethod( "to_string" ), this
235 ) ); 216 ) );
256 } 237 }
257 return tbl; 238 return tbl;
258 } 239 }
259 } 240 }
260 241
261 static final LuanIn stdin = new LuanIn() { 242 public static final LuanIn defaultStdin = new LuanIn() {
262 243
263 @Override InputStream inputStream() { 244 @Override InputStream inputStream() {
264 return System.in; 245 return System.in;
265 } 246 }
266 247
308 289
309 public LuanTable binary_writer() throws IOException { 290 public LuanTable binary_writer() throws IOException {
310 return binaryWriter(new BufferedOutputStream(outputStream())); 291 return binaryWriter(new BufferedOutputStream(outputStream()));
311 } 292 }
312 293
313 @Override LuanTable table() { 294 @Override public LuanTable table() {
314 LuanTable tbl = super.table(); 295 LuanTable tbl = super.table();
315 try { 296 try {
316 tbl.put( "write", new LuanJavaFunction( 297 tbl.put( "write", new LuanJavaFunction(
317 LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this 298 LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this
318 ) ); 299 ) );
346 327
347 public String post(String postS) throws IOException { 328 public String post(String postS) throws IOException {
348 return new UrlCall(url).post(postS); 329 return new UrlCall(url).post(postS);
349 } 330 }
350 331
351 @Override LuanTable table() { 332 @Override public LuanTable table() {
352 LuanTable tbl = super.table(); 333 LuanTable tbl = super.table();
353 try { 334 try {
354 tbl.put( "post", new LuanJavaFunction( 335 tbl.put( "post", new LuanJavaFunction(
355 LuanUrl.class.getMethod( "post", String.class ), this 336 LuanUrl.class.getMethod( "post", String.class ), this
356 ) ); 337 ) );
406 387
407 public boolean rename_to(String dest) { 388 public boolean rename_to(String dest) {
408 return file.renameTo(new File(dest)); 389 return file.renameTo(new File(dest));
409 } 390 }
410 391
411 @Override LuanTable table() { 392 @Override public LuanTable table() {
412 LuanTable tbl = super.table(); 393 LuanTable tbl = super.table();
413 try { 394 try {
414 tbl.put( "name", new LuanJavaFunction( 395 tbl.put( "name", new LuanJavaFunction(
415 File.class.getMethod( "getName" ), file 396 File.class.getMethod( "getName" ), file
416 ) ); 397 ) );
484 } 465 }
485 } 466 }
486 if( url != null ) 467 if( url != null )
487 return new LuanUrl(url).table(); 468 return new LuanUrl(url).table();
488 469
489 // try java
490 if( !isLoading )
491 return null;
492 String modName = name.replace('/','.') + "Luan.LOADER";
493 // check(luan,"classpath",modName);
494 try {
495 //System.out.println("modName = "+modName);
496 final LuanFunction fn = PackageLuan.load_lib(luan,modName); // throws exception if not found
497 LuanFunction loader = new LuanFunction() {
498 @Override public Object call(LuanState luan,Object[] args) {
499 return fn;
500 }
501 };
502 LuanTable tbl = Luan.newTable();
503 tbl.put( "loader", loader );
504 return tbl;
505 } catch(ClassNotFoundException e) {
506 } catch(NoSuchFieldException e) {
507 } catch(IllegalAccessException e) {
508 }
509 return null; 470 return null;
510 } 471 }
511 472
512 private static LuanTable url(String url,Boolean loading) throws IOException { 473 private static LuanTable url(String url,Boolean loading) throws IOException {
513 if( Boolean.TRUE.equals(loading) ) 474 if( Boolean.TRUE.equals(loading) )
546 public static LuanTable stdin(LuanState luan) throws LuanException { 507 public static LuanTable stdin(LuanState luan) throws LuanException {
547 LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io"); 508 LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io");
548 return (LuanTable)io.get("stdin"); 509 return (LuanTable)io.get("stdin");
549 } 510 }
550 511
551 private static LuanTable newSchemes() { 512 public static LuanTable newSchemes() {
552 LuanTable schemes = Luan.newTable(); 513 LuanTable schemes = Luan.newTable();
553 try { 514 try {
554 add( schemes, "file", LuanState.class, String.class, Boolean.class ); 515 add( schemes, "file", LuanState.class, String.class, Boolean.class );
555 add( schemes, "classpath", LuanState.class, String.class, Boolean.class ); 516 add( schemes, "classpath", LuanState.class, String.class, Boolean.class );
556 add( schemes, "socket", LuanState.class, String.class ); 517 add( schemes, "socket", LuanState.class, String.class );
621 InputStream in = new BufferedInputStream(inputStream()); 582 InputStream in = new BufferedInputStream(inputStream());
622 OutputStream out = new BufferedOutputStream(outputStream()); 583 OutputStream out = new BufferedOutputStream(outputStream());
623 new PickleServer(luan,in,out).run(); 584 new PickleServer(luan,in,out).run();
624 } 585 }
625 586
626 @Override LuanTable table() { 587 @Override public LuanTable table() {
627 LuanTable tbl = super.table(); 588 LuanTable tbl = super.table();
628 try { 589 try {
629 tbl.put( "Pickle_client", new LuanJavaFunction( 590 tbl.put( "Pickle_client", new LuanJavaFunction(
630 LuanSocket.class.getMethod( "Pickle_client", LuanState.class ), this 591 LuanSocket.class.getMethod( "Pickle_client", LuanState.class ), this
631 ) ); 592 ) );