Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 116:1ff1c32417eb
more IoLib work and added init.luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@117 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 30 May 2014 04:55:37 +0000 |
parents | eacf6ce1b47d |
children | 735708619119 |
comparison
equal
deleted
inserted
replaced
115:eacf6ce1b47d | 116:1ff1c32417eb |
---|---|
39 add( global, "get_metatable", LuanState.class, Object.class ); | 39 add( global, "get_metatable", LuanState.class, Object.class ); |
40 add( global, "ipairs", LuanState.class, LuanTable.class ); | 40 add( global, "ipairs", LuanState.class, LuanTable.class ); |
41 add( global, "load", LuanState.class, String.class, String.class, Boolean.class ); | 41 add( global, "load", LuanState.class, String.class, String.class, Boolean.class ); |
42 add( global, "load_file", LuanState.class, String.class ); | 42 add( global, "load_file", LuanState.class, String.class ); |
43 add( global, "pairs", LuanState.class, LuanTable.class ); | 43 add( global, "pairs", LuanState.class, LuanTable.class ); |
44 add( global, "print", LuanState.class, new Object[0].getClass() ); | 44 // add( global, "print", LuanState.class, new Object[0].getClass() ); |
45 add( global, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class ); | 45 add( global, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class ); |
46 add( global, "raw_equal", Object.class, Object.class ); | 46 add( global, "raw_equal", Object.class, Object.class ); |
47 add( global, "raw_get", LuanTable.class, Object.class ); | 47 add( global, "raw_get", LuanTable.class, Object.class ); |
48 add( global, "raw_len", LuanState.class, Object.class ); | 48 add( global, "raw_len", LuanState.class, Object.class ); |
49 add( global, "raw_set", LuanTable.class, Object.class, Object.class ); | 49 add( global, "raw_set", LuanTable.class, Object.class, Object.class ); |
51 add( global, "set_metatable", LuanTable.class, LuanTable.class ); | 51 add( global, "set_metatable", LuanTable.class, LuanTable.class ); |
52 add( global, "to_number", Object.class, Integer.class ); | 52 add( global, "to_number", Object.class, Integer.class ); |
53 add( global, "to_string", LuanState.class, Object.class ); | 53 add( global, "to_string", LuanState.class, Object.class ); |
54 add( global, "type", Object.class ); | 54 add( global, "type", Object.class ); |
55 global.put( "_VERSION", Luan.version ); | 55 global.put( "_VERSION", Luan.version ); |
56 add( module, "values", new Object[0].getClass() ); | |
56 } catch(NoSuchMethodException e) { | 57 } catch(NoSuchMethodException e) { |
57 throw new RuntimeException(e); | 58 throw new RuntimeException(e); |
58 } | 59 } |
59 return module; | 60 return module; |
60 } | 61 } |
61 }; | 62 }; |
62 | 63 |
63 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 64 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
64 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); | 65 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); |
65 } | 66 } |
66 | 67 /* |
67 public static void print(LuanState luan,Object... args) throws LuanException { | 68 public static void print(LuanState luan,Object... args) throws LuanException { |
68 LuanFunction write = (LuanFunction)luan.get("Io.stdout.write"); | 69 LuanFunction write = (LuanFunction)luan.get("Io.stdout.write"); |
69 List list = new ArrayList(); | 70 List<Object> list = new ArrayList<Object>(); |
70 for( int i=0; i<args.length; i++ ) { | 71 for( int i=0; i<args.length; i++ ) { |
71 if( i > 0 ) | 72 if( i > 0 ) |
72 list.add("\t"); | 73 list.add("\t"); |
73 list.add(args[i]); | 74 list.add(args[i]); |
74 } | 75 } |
75 list.add("\n"); | 76 list.add("\n"); |
76 write.call(luan,list.toArray()); | 77 write.call(luan,list.toArray()); |
77 } | 78 } |
78 | 79 */ |
79 public static String type(Object obj) { | 80 public static String type(Object obj) { |
80 return Luan.type(obj); | 81 return Luan.type(obj); |
81 } | 82 } |
82 | 83 |
83 public static LuanFunction load(LuanState luan,String text,String sourceName,Boolean interactive) throws LuanException { | 84 public static LuanFunction load(LuanState luan,String text,String sourceName,Boolean interactive) throws LuanException { |
86 else | 87 else |
87 return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text)); | 88 return LuanCompiler.compileModule(luan,new LuanSource(sourceName,text)); |
88 } | 89 } |
89 | 90 |
90 | 91 |
91 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException, IOException { | 92 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { |
92 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); | 93 try { |
93 return load(luan,src,fileName,false); | 94 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); |
94 } | 95 return load(luan,src,fileName,false); |
95 | 96 } catch(IOException e) { |
96 public static Object do_file(LuanState luan,String fileName) throws LuanException, IOException { | 97 throw luan.JAVA.exception(e); |
98 } | |
99 } | |
100 | |
101 public static LuanFunction load_java_resource(LuanState luan,String path) throws LuanException { | |
102 try { | |
103 String src = new IoLib.LuanUrl(IoLib.java_resource_to_url(path)).read_text(); | |
104 return load(luan,src,path,false); | |
105 } catch(IOException e) { | |
106 throw luan.JAVA.exception(e); | |
107 } | |
108 } | |
109 | |
110 public static Object do_file(LuanState luan,String fileName) throws LuanException { | |
97 LuanFunction fn = load_file(luan,fileName); | 111 LuanFunction fn = load_file(luan,fileName); |
112 return luan.JAVA.call(fn,null); | |
113 } | |
114 | |
115 public static Object do_java_resource(LuanState luan,String path) throws LuanException { | |
116 LuanFunction fn = load_java_resource(luan,path); | |
98 return luan.JAVA.call(fn,null); | 117 return luan.JAVA.call(fn,null); |
99 } | 118 } |
100 | 119 |
101 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 120 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { |
102 return new LuanFunction() { | 121 return new LuanFunction() { |
217 return rtn; | 236 return rtn; |
218 } | 237 } |
219 }; | 238 }; |
220 } | 239 } |
221 | 240 |
241 public static LuanFunction values(final Object... args) throws LuanException { | |
242 return new LuanFunction() { | |
243 int i = 0; | |
244 | |
245 @Override public Object call(LuanState luan,Object[] unused) { | |
246 return i < args.length ? args[i++] : null; | |
247 } | |
248 }; | |
249 } | |
250 | |
222 } | 251 } |