Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 130:0594c132888b
cleanup
git-svn-id: https://luan-java.googlecode.com/svn/trunk@131 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 10 Jun 2014 02:43:40 +0000 |
parents | f0a4abe58593 |
children | 15a8e6588f3c |
comparison
equal
deleted
inserted
replaced
129:486a0641bca4 | 130:0594c132888b |
---|---|
41 add( global, "get_metatable", LuanState.class, Object.class ); | 41 add( global, "get_metatable", LuanState.class, Object.class ); |
42 add( global, "ipairs", LuanState.class, LuanTable.class ); | 42 add( global, "ipairs", LuanState.class, LuanTable.class ); |
43 add( global, "load", LuanState.class, String.class, String.class, Boolean.class, Boolean.class ); | 43 add( global, "load", LuanState.class, String.class, String.class, Boolean.class, Boolean.class ); |
44 add( global, "load_file", LuanState.class, String.class ); | 44 add( global, "load_file", LuanState.class, String.class ); |
45 add( global, "pairs", LuanState.class, LuanTable.class ); | 45 add( global, "pairs", LuanState.class, LuanTable.class ); |
46 // add( global, "print", LuanState.class, new Object[0].getClass() ); | |
47 add( global, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class ); | 46 add( global, "range", LuanState.class, Double.TYPE, Double.TYPE, Double.class ); |
48 add( global, "raw_equal", Object.class, Object.class ); | 47 add( global, "raw_equal", Object.class, Object.class ); |
49 add( global, "raw_get", LuanTable.class, Object.class ); | 48 add( global, "raw_get", LuanTable.class, Object.class ); |
50 add( global, "raw_len", LuanState.class, Object.class ); | 49 add( global, "raw_len", LuanState.class, Object.class ); |
51 add( global, "raw_set", LuanTable.class, Object.class, Object.class ); | 50 add( global, "raw_set", LuanTable.class, Object.class, Object.class ); |
64 }; | 63 }; |
65 | 64 |
66 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { | 65 private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { |
67 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); | 66 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); |
68 } | 67 } |
69 /* | 68 |
70 public static void print(LuanState luan,Object... args) throws LuanException { | |
71 LuanFunction write = (LuanFunction)luan.get("Io.stdout.write"); | |
72 List<Object> list = new ArrayList<Object>(); | |
73 for( int i=0; i<args.length; i++ ) { | |
74 if( i > 0 ) | |
75 list.add("\t"); | |
76 list.add(args[i]); | |
77 } | |
78 list.add("\n"); | |
79 write.call(luan,list.toArray()); | |
80 } | |
81 */ | |
82 public static String type(Object obj) { | 69 public static String type(Object obj) { |
83 return Luan.type(obj); | 70 return Luan.type(obj); |
84 } | 71 } |
85 | 72 |
86 public static LuanFunction load(LuanState luan,String text,String sourceName,Boolean useGlobal,Boolean allowExpr) | 73 public static LuanFunction load(LuanState luan,String text,String sourceName,Boolean useGlobal,Boolean allowExpr) |
97 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { | 84 public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { |
98 try { | 85 try { |
99 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : new IoLib.LuanFile(fileName).read_text(); | 86 String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : new IoLib.LuanFile(fileName).read_text(); |
100 return load(luan,src,fileName,false,false); | 87 return load(luan,src,fileName,false,false); |
101 } catch(IOException e) { | 88 } catch(IOException e) { |
102 throw luan.JAVA.exception(e); | 89 throw luan.exception(e); |
103 } | 90 } |
104 } | 91 } |
105 | 92 |
106 public static LuanFunction load_java_resource(LuanState luan,String path) throws LuanException { | 93 public static LuanFunction load_java_resource(LuanState luan,String path) throws LuanException { |
107 try { | 94 try { |
108 String src = new IoLib.LuanUrl(IoLib.java_resource_to_url(path)).read_text(); | 95 String src = new IoLib.LuanUrl(IoLib.java_resource_to_url(path)).read_text(); |
109 return load(luan,src,path,false,false); | 96 return load(luan,src,path,false,false); |
110 } catch(IOException e) { | 97 } catch(IOException e) { |
111 throw luan.JAVA.exception(e); | 98 throw luan.exception(e); |
112 } | 99 } |
113 } | 100 } |
114 | 101 |
115 public static Object do_file(LuanState luan,String fileName) throws LuanException { | 102 public static Object do_file(LuanState luan,String fileName) throws LuanException { |
116 LuanFunction fn = load_file(luan,fileName); | 103 LuanFunction fn = load_file(luan,fileName); |
117 return luan.JAVA.call(fn,null); | 104 return luan.call(fn); |
118 } | 105 } |
119 | 106 |
120 public static Object do_java_resource(LuanState luan,String path) throws LuanException { | 107 public static Object do_java_resource(LuanState luan,String path) throws LuanException { |
121 LuanFunction fn = load_java_resource(luan,path); | 108 LuanFunction fn = load_java_resource(luan,path); |
122 return luan.JAVA.call(fn,null); | 109 return luan.call(fn); |
123 } | 110 } |
124 | 111 |
125 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 112 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { |
126 return new LuanFunction() { | 113 return new LuanFunction() { |
127 @Override public Object[] call(LuanState luan,Object[] args) { | 114 @Override public Object[] call(LuanState luan,Object[] args) { |
172 } | 159 } |
173 if( v instanceof LuanTable ) { | 160 if( v instanceof LuanTable ) { |
174 LuanTable t = (LuanTable)v; | 161 LuanTable t = (LuanTable)v; |
175 return t.length(); | 162 return t.length(); |
176 } | 163 } |
177 throw luan.JAVA.exception( "bad argument #1 to 'raw_len' (table or string expected)" ); | 164 throw luan.exception( "bad argument #1 to 'raw_len' (table or string expected)" ); |
178 } | 165 } |
179 | 166 |
180 public static Number to_number(Object e,Integer base) { | 167 public static Number to_number(Object e,Integer base) { |
181 return Luan.toNumber(e,base); | 168 return Luan.toNumber(e,base); |
182 } | 169 } |
183 | 170 |
184 public static String to_string(LuanState luan,Object v) throws LuanException { | 171 public static String to_string(LuanState luan,Object v) throws LuanException { |
185 return luan.JAVA.toString(v); | 172 return luan.toString(v); |
186 } | 173 } |
187 | 174 |
188 public static void error(LuanState luan,Object msg) throws LuanException { | 175 public static void error(LuanState luan,Object msg) throws LuanException { |
189 throw luan.JAVA.exception(msg); | 176 throw luan.exception(msg); |
190 } | 177 } |
191 | 178 |
192 public static Object assert_(LuanState luan,Object v,String msg) throws LuanException { | 179 public static Object assert_(LuanState luan,Object v,String msg) throws LuanException { |
193 if( Luan.toBoolean(v) ) | 180 if( Luan.toBoolean(v) ) |
194 return v; | 181 return v; |
195 if( msg == null ) | 182 if( msg == null ) |
196 msg = "assertion failed!"; | 183 msg = "assertion failed!"; |
197 throw luan.JAVA.exception( msg ); | 184 throw luan.exception( msg ); |
198 } | 185 } |
199 | 186 |
200 public static String assert_string(LuanState luan,String v) throws LuanException { | 187 public static String assert_string(LuanState luan,String v) throws LuanException { |
201 Utils.checkNotNull(luan,v,"string"); | 188 Utils.checkNotNull(luan,v,"string"); |
202 return v; | 189 return v; |
216 return v; | 203 return v; |
217 } | 204 } |
218 | 205 |
219 public static Object assert_nil(LuanState luan,Object v) throws LuanException { | 206 public static Object assert_nil(LuanState luan,Object v) throws LuanException { |
220 if( v != null ) | 207 if( v != null ) |
221 throw luan.JAVA.exception("bad argument #1 (nil expected, got "+Luan.type(v)+")"); | 208 throw luan.exception("bad argument #1 (nil expected, got "+Luan.type(v)+")"); |
222 return v; | 209 return v; |
223 } | 210 } |
224 | 211 |
225 public static String repr(LuanState luan,Object v) throws LuanException { | 212 public static String repr(LuanState luan,Object v) throws LuanException { |
226 return luan.JAVA.repr(v); | 213 return luan.repr(v); |
227 } | 214 } |
228 | 215 |
229 public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException { | 216 public static LuanFunction range(LuanState luan,final double from,final double to,Double stepV) throws LuanException { |
230 final double step = stepV==null ? 1.0 : stepV; | 217 final double step = stepV==null ? 1.0 : stepV; |
231 if( step == 0.0 ) | 218 if( step == 0.0 ) |
232 throw luan.JAVA.exception("bad argument #3 (step may not be zero)"); | 219 throw luan.exception("bad argument #3 (step may not be zero)"); |
233 return new LuanFunction() { | 220 return new LuanFunction() { |
234 double v = from; | 221 double v = from; |
235 | 222 |
236 @Override public Object call(LuanState luan,Object[] args) { | 223 @Override public Object call(LuanState luan,Object[] args) { |
237 if( step > 0.0 && v > to || step < 0.0 && v < to ) | 224 if( step > 0.0 && v > to || step < 0.0 && v < to ) |