Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 36:2a35154aec14
implement more basic lib functions
git-svn-id: https://luan-java.googlecode.com/svn/trunk@37 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 18 Dec 2012 09:53:42 +0000 |
parents | e51906de0f11 |
children | 8a57ebfdfd78 |
comparison
equal
deleted
inserted
replaced
35:e51906de0f11 | 36:2a35154aec14 |
---|---|
1 package luan.lib; | 1 package luan.lib; |
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.Reader; | 4 import java.io.Reader; |
5 import java.io.FileReader; | 5 import java.io.FileReader; |
6 import java.io.InputStreamReader; | |
6 import java.io.IOException; | 7 import java.io.IOException; |
7 import java.lang.reflect.Method; | 8 import java.lang.reflect.Method; |
8 import java.util.Iterator; | 9 import java.util.Iterator; |
9 import java.util.Map; | 10 import java.util.Map; |
10 import luan.Lua; | 11 import luan.Lua; |
25 add( t, "getmetatable", Object.class ); | 26 add( t, "getmetatable", Object.class ); |
26 add( t, "ipairs", LuaTable.class ); | 27 add( t, "ipairs", LuaTable.class ); |
27 add( t, "load", LuaState.class, String.class ); | 28 add( t, "load", LuaState.class, String.class ); |
28 add( t, "loadfile", LuaState.class, String.class ); | 29 add( t, "loadfile", LuaState.class, String.class ); |
29 add( t, "pairs", LuaTable.class ); | 30 add( t, "pairs", LuaTable.class ); |
30 add( t, "print", new Object[0].getClass() ); | 31 add( t, "print", LuaState.class, new Object[0].getClass() ); |
32 add( t, "rawequal", Object.class, Object.class ); | |
33 add( t, "rawget", LuaTable.class, Object.class ); | |
34 add( t, "rawlen", Object.class ); | |
35 add( t, "rawset", LuaTable.class, Object.class, Object.class ); | |
31 add( t, "setmetatable", LuaTable.class, LuaTable.class ); | 36 add( t, "setmetatable", LuaTable.class, LuaTable.class ); |
37 add( t, "tonumber", Object.class, Integer.class ); | |
38 add( t, "tostring", LuaState.class, Object.class ); | |
32 add( t, "type", Object.class ); | 39 add( t, "type", Object.class ); |
33 t.put( "_VERSION", Lua.version ); | 40 t.put( "_VERSION", Lua.version ); |
34 } | 41 } |
35 | 42 |
36 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { | 43 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { |
39 } catch(NoSuchMethodException e) { | 46 } catch(NoSuchMethodException e) { |
40 throw new RuntimeException(e); | 47 throw new RuntimeException(e); |
41 } | 48 } |
42 } | 49 } |
43 | 50 |
44 public static void print(Object... args) { | 51 public static void print(LuaState lua,Object... args) throws LuaException { |
45 for( int i=0; i<args.length; i++ ) { | 52 for( int i=0; i<args.length; i++ ) { |
46 if( i > 0 ) | 53 if( i > 0 ) |
47 System.out.print('\t'); | 54 System.out.print('\t'); |
48 System.out.print( Lua.toString(args[i]) ); | 55 System.out.print( lua.toString(args[i]) ); |
49 } | 56 } |
50 System.out.println(); | 57 System.out.println(); |
51 } | 58 } |
52 | 59 |
53 public static String type(Object obj) { | 60 public static String type(Object obj) { |
79 return s; | 86 return s; |
80 } | 87 } |
81 | 88 |
82 | 89 |
83 public static LuaFunction loadfile(LuaState lua,String fileName) throws LuaException,IOException { | 90 public static LuaFunction loadfile(LuaState lua,String fileName) throws LuaException,IOException { |
84 return load(lua,read(new File(fileName))); | 91 String src = fileName==null ? readAll(new InputStreamReader(System.in)) : read(new File(fileName)); |
92 return load(lua,src); | |
93 } | |
94 | |
95 public static Object[] dofile(LuaState lua,String fileName) throws LuaException,IOException { | |
96 return loadfile(lua,fileName).call(lua); | |
85 } | 97 } |
86 | 98 |
87 private static class TableIter { | 99 private static class TableIter { |
88 private final Iterator<Map.Entry<Object,Object>> iter; | 100 private final Iterator<Map.Entry<Object,Object>> iter; |
89 | 101 |
142 | 154 |
143 public static LuaTable setmetatable(LuaTable table,LuaTable metatable) { | 155 public static LuaTable setmetatable(LuaTable table,LuaTable metatable) { |
144 table.setMetatable(metatable); | 156 table.setMetatable(metatable); |
145 return table; | 157 return table; |
146 } | 158 } |
159 | |
160 public static boolean rawequal(Object v1,Object v2) { | |
161 return v1 == v2 || v1 != null && v1.equals(v2); | |
162 } | |
163 | |
164 public static Object rawget(LuaTable table,Object index) { | |
165 return table.get(index); | |
166 } | |
167 | |
168 public static LuaTable rawset(LuaTable table,Object index,Object value) { | |
169 table.put(index,value); | |
170 return table; | |
171 } | |
172 | |
173 public static int rawlen(Object v) throws LuaException { | |
174 if( v instanceof String ) { | |
175 String s = (String)v; | |
176 return s.length(); | |
177 } | |
178 if( v instanceof LuaTable ) { | |
179 LuaTable t = (LuaTable)v; | |
180 return t.length(); | |
181 } | |
182 throw new LuaException( "bad argument #1 to 'rawlen' (table or string expected)" ); | |
183 } | |
184 | |
185 public static LuaNumber tonumber(Object e,Integer base) { | |
186 return Lua.toNumber(e,base); | |
187 } | |
188 | |
189 public static String tostring(LuaState lua,Object v) throws LuaException { | |
190 return lua.toString(v); | |
191 } | |
147 } | 192 } |