comparison core/src/luan/modules/IoLuan.java @ 427:dae264ad6a7b

fix LuanTable.put() to use metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2015 19:29:07 -0600
parents 23a93c118042
children 3ffe8ba5b297
comparison
equal deleted inserted replaced
426:23a93c118042 427:dae264ad6a7b
36 36
37 37
38 public final class IoLuan { 38 public final class IoLuan {
39 39
40 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { 40 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
41 t.put( method, new LuanJavaFunction(IoLuan.class.getMethod(method,parameterTypes),null) ); 41 t.rawPut( method, new LuanJavaFunction(IoLuan.class.getMethod(method,parameterTypes),null) );
42 } 42 }
43 43
44 public static String read_console_line(String prompt) throws IOException { 44 public static String read_console_line(String prompt) throws IOException {
45 if( prompt==null ) 45 if( prompt==null )
46 prompt = "> "; 46 prompt = "> ";
86 } 86 }
87 87
88 private static LuanTable writer(LuanWriter luanWriter) { 88 private static LuanTable writer(LuanWriter luanWriter) {
89 LuanTable writer = new LuanTable(); 89 LuanTable writer = new LuanTable();
90 try { 90 try {
91 writer.put( "write", new LuanJavaFunction( 91 writer.rawPut( "write", new LuanJavaFunction(
92 LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter 92 LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter
93 ) ); 93 ) );
94 writer.put( "close", new LuanJavaFunction( 94 writer.rawPut( "close", new LuanJavaFunction(
95 LuanWriter.class.getMethod( "close" ), luanWriter 95 LuanWriter.class.getMethod( "close" ), luanWriter
96 ) ); 96 ) );
97 } catch(NoSuchMethodException e) { 97 } catch(NoSuchMethodException e) {
98 throw new RuntimeException(e); 98 throw new RuntimeException(e);
99 } 99 }
102 102
103 103
104 public static LuanTable binaryWriter(final OutputStream out) { 104 public static LuanTable binaryWriter(final OutputStream out) {
105 LuanTable writer = new LuanTable(); 105 LuanTable writer = new LuanTable();
106 try { 106 try {
107 writer.put( "write", new LuanJavaFunction( 107 writer.rawPut( "write", new LuanJavaFunction(
108 OutputStream.class.getMethod( "write", new byte[0].getClass() ), out 108 OutputStream.class.getMethod( "write", new byte[0].getClass() ), out
109 ) ); 109 ) );
110 writer.put( "close", new LuanJavaFunction( 110 writer.rawPut( "close", new LuanJavaFunction(
111 OutputStream.class.getMethod( "close" ), out 111 OutputStream.class.getMethod( "close" ), out
112 ) ); 112 ) );
113 } catch(NoSuchMethodException e) { 113 } catch(NoSuchMethodException e) {
114 throw new RuntimeException(e); 114 throw new RuntimeException(e);
115 } 115 }
204 } 204 }
205 205
206 public LuanTable table() { 206 public LuanTable table() {
207 LuanTable tbl = new LuanTable(); 207 LuanTable tbl = new LuanTable();
208 try { 208 try {
209 tbl.put( "to_string", new LuanJavaFunction( 209 tbl.rawPut( "to_string", new LuanJavaFunction(
210 LuanIn.class.getMethod( "to_string" ), this 210 LuanIn.class.getMethod( "to_string" ), this
211 ) ); 211 ) );
212 tbl.put( "read_text", new LuanJavaFunction( 212 tbl.rawPut( "read_text", new LuanJavaFunction(
213 LuanIn.class.getMethod( "read_text" ), this 213 LuanIn.class.getMethod( "read_text" ), this
214 ) ); 214 ) );
215 tbl.put( "read_binary", new LuanJavaFunction( 215 tbl.rawPut( "read_binary", new LuanJavaFunction(
216 LuanIn.class.getMethod( "read_binary" ), this 216 LuanIn.class.getMethod( "read_binary" ), this
217 ) ); 217 ) );
218 tbl.put( "read_lines", new LuanJavaFunction( 218 tbl.rawPut( "read_lines", new LuanJavaFunction(
219 LuanIn.class.getMethod( "read_lines" ), this 219 LuanIn.class.getMethod( "read_lines" ), this
220 ) ); 220 ) );
221 tbl.put( "read_blocks", new LuanJavaFunction( 221 tbl.rawPut( "read_blocks", new LuanJavaFunction(
222 LuanIn.class.getMethod( "read_blocks", Integer.class ), this 222 LuanIn.class.getMethod( "read_blocks", Integer.class ), this
223 ) ); 223 ) );
224 tbl.put( "exists", new LuanJavaFunction( 224 tbl.rawPut( "exists", new LuanJavaFunction(
225 LuanIn.class.getMethod( "exists" ), this 225 LuanIn.class.getMethod( "exists" ), this
226 ) ); 226 ) );
227 } catch(NoSuchMethodException e) { 227 } catch(NoSuchMethodException e) {
228 throw new RuntimeException(e); 228 throw new RuntimeException(e);
229 } 229 }
284 } 284 }
285 285
286 @Override public LuanTable table() { 286 @Override public LuanTable table() {
287 LuanTable tbl = super.table(); 287 LuanTable tbl = super.table();
288 try { 288 try {
289 tbl.put( "write", new LuanJavaFunction( 289 tbl.rawPut( "write", new LuanJavaFunction(
290 LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this 290 LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this
291 ) ); 291 ) );
292 tbl.put( "text_writer", new LuanJavaFunction( 292 tbl.rawPut( "text_writer", new LuanJavaFunction(
293 LuanIO.class.getMethod( "text_writer" ), this 293 LuanIO.class.getMethod( "text_writer" ), this
294 ) ); 294 ) );
295 tbl.put( "binary_writer", new LuanJavaFunction( 295 tbl.rawPut( "binary_writer", new LuanJavaFunction(
296 LuanIO.class.getMethod( "binary_writer" ), this 296 LuanIO.class.getMethod( "binary_writer" ), this
297 ) ); 297 ) );
298 } catch(NoSuchMethodException e) { 298 } catch(NoSuchMethodException e) {
299 throw new RuntimeException(e); 299 throw new RuntimeException(e);
300 } 300 }
371 } 371 }
372 372
373 @Override public LuanTable table() { 373 @Override public LuanTable table() {
374 LuanTable tbl = super.table(); 374 LuanTable tbl = super.table();
375 try { 375 try {
376 tbl.put( "post", new LuanJavaFunction( 376 tbl.rawPut( "post", new LuanJavaFunction(
377 LuanUrl.class.getMethod( "post", String.class ), this 377 LuanUrl.class.getMethod( "post", String.class ), this
378 ) ); 378 ) );
379 } catch(NoSuchMethodException e) { 379 } catch(NoSuchMethodException e) {
380 throw new RuntimeException(e); 380 throw new RuntimeException(e);
381 } 381 }
442 } 442 }
443 443
444 @Override public LuanTable table() { 444 @Override public LuanTable table() {
445 LuanTable tbl = super.table(); 445 LuanTable tbl = super.table();
446 try { 446 try {
447 tbl.put( "name", new LuanJavaFunction( 447 tbl.rawPut( "name", new LuanJavaFunction(
448 File.class.getMethod( "getName" ), file 448 File.class.getMethod( "getName" ), file
449 ) ); 449 ) );
450 tbl.put( "is_directory", new LuanJavaFunction( 450 tbl.rawPut( "is_directory", new LuanJavaFunction(
451 File.class.getMethod( "isDirectory" ), file 451 File.class.getMethod( "isDirectory" ), file
452 ) ); 452 ) );
453 tbl.put( "is_file", new LuanJavaFunction( 453 tbl.rawPut( "is_file", new LuanJavaFunction(
454 File.class.getMethod( "isFile" ), file 454 File.class.getMethod( "isFile" ), file
455 ) ); 455 ) );
456 tbl.put( "delete", new LuanJavaFunction( 456 tbl.rawPut( "delete", new LuanJavaFunction(
457 File.class.getMethod( "delete" ), file 457 File.class.getMethod( "delete" ), file
458 ) ); 458 ) );
459 tbl.put( "mkdir", new LuanJavaFunction( 459 tbl.rawPut( "mkdir", new LuanJavaFunction(
460 File.class.getMethod( "mkdir" ), file 460 File.class.getMethod( "mkdir" ), file
461 ) ); 461 ) );
462 tbl.put( "mkdirs", new LuanJavaFunction( 462 tbl.rawPut( "mkdirs", new LuanJavaFunction(
463 File.class.getMethod( "mkdirs" ), file 463 File.class.getMethod( "mkdirs" ), file
464 ) ); 464 ) );
465 tbl.put( "last_modified", new LuanJavaFunction( 465 tbl.rawPut( "last_modified", new LuanJavaFunction(
466 File.class.getMethod( "lastModified" ), file 466 File.class.getMethod( "lastModified" ), file
467 ) ); 467 ) );
468 tbl.put( "child", new LuanJavaFunction( 468 tbl.rawPut( "child", new LuanJavaFunction(
469 LuanFile.class.getMethod( "child", LuanState.class, String.class ), this 469 LuanFile.class.getMethod( "child", LuanState.class, String.class ), this
470 ) ); 470 ) );
471 tbl.put( "children", new LuanJavaFunction( 471 tbl.rawPut( "children", new LuanJavaFunction(
472 LuanFile.class.getMethod( "children", LuanState.class ), this 472 LuanFile.class.getMethod( "children", LuanState.class ), this
473 ) ); 473 ) );
474 tbl.put( "parent", new LuanJavaFunction( 474 tbl.rawPut( "parent", new LuanJavaFunction(
475 LuanFile.class.getMethod( "parent", LuanState.class ), this 475 LuanFile.class.getMethod( "parent", LuanState.class ), this
476 ) ); 476 ) );
477 tbl.put( "rename_to", new LuanJavaFunction( 477 tbl.rawPut( "rename_to", new LuanJavaFunction(
478 LuanFile.class.getMethod( "rename_to", String.class ), this 478 LuanFile.class.getMethod( "rename_to", String.class ), this
479 ) ); 479 ) );
480 tbl.put( "canonical", new LuanJavaFunction( 480 tbl.rawPut( "canonical", new LuanJavaFunction(
481 LuanFile.class.getMethod( "canonical", LuanState.class ), this 481 LuanFile.class.getMethod( "canonical", LuanState.class ), this
482 ) ); 482 ) );
483 } catch(NoSuchMethodException e) { 483 } catch(NoSuchMethodException e) {
484 throw new RuntimeException(e); 484 throw new RuntimeException(e);
485 } 485 }
625 } 625 }
626 626
627 @Override public LuanTable table() { 627 @Override public LuanTable table() {
628 LuanTable tbl = super.table(); 628 LuanTable tbl = super.table();
629 try { 629 try {
630 tbl.put( "Pickle_client", new LuanJavaFunction( 630 tbl.rawPut( "Pickle_client", new LuanJavaFunction(
631 LuanSocket.class.getMethod( "Pickle_client", LuanState.class ), this 631 LuanSocket.class.getMethod( "Pickle_client", LuanState.class ), this
632 ) ); 632 ) );
633 tbl.put( "run_pickle_server", new LuanJavaFunction( 633 tbl.rawPut( "run_pickle_server", new LuanJavaFunction(
634 LuanSocket.class.getMethod( "run_pickle_server", LuanState.class ), this 634 LuanSocket.class.getMethod( "run_pickle_server", LuanState.class ), this
635 ) ); 635 ) );
636 } catch(NoSuchMethodException e) { 636 } catch(NoSuchMethodException e) {
637 throw new RuntimeException(e); 637 throw new RuntimeException(e);
638 } 638 }
683 if( s!=null ) 683 if( s!=null )
684 s.check(luan,name); 684 s.check(luan,name);
685 } 685 }
686 686
687 public static void setSecurity(LuanState luan,Security s) { 687 public static void setSecurity(LuanState luan,Security s) {
688 luan.registry().put(SECURITY_KEY,s); 688 luan.registry().rawPut(SECURITY_KEY,s);
689 } 689 }
690 690
691 private void IoLuan() {} // never 691 private void IoLuan() {} // never
692 } 692 }