Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 48:64ecb7a3aad7
rename Lua to Luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@49 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 28 Dec 2012 03:29:12 +0000 |
parents | a443637829c1 |
children | 8ede219cd111 |
comparison
equal
deleted
inserted
replaced
47:659c7139e903 | 48:64ecb7a3aad7 |
---|---|
6 import java.io.InputStreamReader; | 6 import java.io.InputStreamReader; |
7 import java.io.IOException; | 7 import java.io.IOException; |
8 import java.lang.reflect.Method; | 8 import java.lang.reflect.Method; |
9 import java.util.Iterator; | 9 import java.util.Iterator; |
10 import java.util.Map; | 10 import java.util.Map; |
11 import luan.Lua; | 11 import luan.Luan; |
12 import luan.LuaState; | 12 import luan.LuanState; |
13 import luan.LuaTable; | 13 import luan.LuanTable; |
14 import luan.LuaFunction; | 14 import luan.LuanFunction; |
15 import luan.LuaJavaFunction; | 15 import luan.LuanJavaFunction; |
16 import luan.LuaException; | 16 import luan.LuanException; |
17 import luan.LuaSource; | 17 import luan.LuanSource; |
18 import luan.LuaElement; | 18 import luan.LuanElement; |
19 import luan.interp.LuaCompiler; | 19 import luan.interp.LuanCompiler; |
20 | 20 |
21 | 21 |
22 public final class BasicLib { | 22 public final class BasicLib { |
23 | 23 |
24 public static void register(LuaState lua) { | 24 public static void register(LuanState lua) { |
25 LuaTable global = lua.global(); | 25 LuanTable global = lua.global(); |
26 global.put( "_G", global ); | 26 global.put( "_G", global ); |
27 add( global, "do_file", LuaState.class, String.class ); | 27 add( global, "do_file", LuanState.class, String.class ); |
28 add( global, "error", LuaState.class, Object.class ); | 28 add( global, "error", LuanState.class, Object.class ); |
29 add( global, "get_metatable", LuaState.class, Object.class ); | 29 add( global, "get_metatable", LuanState.class, Object.class ); |
30 add( global, "ipairs", LuaTable.class ); | 30 add( global, "ipairs", LuanTable.class ); |
31 add( global, "load", LuaState.class, String.class, String.class ); | 31 add( global, "load", LuanState.class, String.class, String.class ); |
32 add( global, "load_file", LuaState.class, String.class ); | 32 add( global, "load_file", LuanState.class, String.class ); |
33 add( global, "pairs", LuaTable.class ); | 33 add( global, "pairs", LuanTable.class ); |
34 add( global, "print", LuaState.class, new Object[0].getClass() ); | 34 add( global, "print", LuanState.class, new Object[0].getClass() ); |
35 add( global, "raw_equal", Object.class, Object.class ); | 35 add( global, "raw_equal", Object.class, Object.class ); |
36 add( global, "raw_get", LuaTable.class, Object.class ); | 36 add( global, "raw_get", LuanTable.class, Object.class ); |
37 add( global, "raw_len", LuaState.class, Object.class ); | 37 add( global, "raw_len", LuanState.class, Object.class ); |
38 add( global, "raw_set", LuaTable.class, Object.class, Object.class ); | 38 add( global, "raw_set", LuanTable.class, Object.class, Object.class ); |
39 add( global, "set_metatable", LuaTable.class, LuaTable.class ); | 39 add( global, "set_metatable", LuanTable.class, LuanTable.class ); |
40 add( global, "to_number", Object.class, Integer.class ); | 40 add( global, "to_number", Object.class, Integer.class ); |
41 add( global, "to_string", LuaState.class, Object.class ); | 41 add( global, "to_string", LuanState.class, Object.class ); |
42 add( global, "type", Object.class ); | 42 add( global, "type", Object.class ); |
43 global.put( "_VERSION", Lua.version ); | 43 global.put( "_VERSION", Luan.version ); |
44 | 44 |
45 add( global, "make_standard", LuaState.class ); | 45 add( global, "make_standard", LuanState.class ); |
46 } | 46 } |
47 | 47 |
48 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { | 48 private static void add(LuanTable t,String method,Class<?>... parameterTypes) { |
49 try { | 49 try { |
50 t.put( method, new LuaJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); | 50 t.put( method, new LuanJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); |
51 } catch(NoSuchMethodException e) { | 51 } catch(NoSuchMethodException e) { |
52 throw new RuntimeException(e); | 52 throw new RuntimeException(e); |
53 } | 53 } |
54 } | 54 } |
55 | 55 |
56 public static void make_standard(LuaState lua) { | 56 public static void make_standard(LuanState lua) { |
57 LuaTable global = lua.global(); | 57 LuanTable global = lua.global(); |
58 global.put( "dofile", global.get("do_file") ); | 58 global.put( "dofile", global.get("do_file") ); |
59 global.put( "getmetatable", global.get("get_metatable") ); | 59 global.put( "getmetatable", global.get("get_metatable") ); |
60 global.put( "loadfile", global.get("load_file") ); | 60 global.put( "loadfile", global.get("load_file") ); |
61 global.put( "rawequal", global.get("raw_equal") ); | 61 global.put( "rawequal", global.get("raw_equal") ); |
62 global.put( "rawget", global.get("raw_get") ); | 62 global.put( "rawget", global.get("raw_get") ); |
65 global.put( "setmetatable", global.get("set_metatable") ); | 65 global.put( "setmetatable", global.get("set_metatable") ); |
66 global.put( "tonumber", global.get("to_number") ); | 66 global.put( "tonumber", global.get("to_number") ); |
67 global.put( "tostring", global.get("to_string") ); | 67 global.put( "tostring", global.get("to_string") ); |
68 } | 68 } |
69 | 69 |
70 public static void print(LuaState lua,Object... args) throws LuaException { | 70 public static void print(LuanState lua,Object... args) throws LuanException { |
71 for( int i=0; i<args.length; i++ ) { | 71 for( int i=0; i<args.length; i++ ) { |
72 if( i > 0 ) | 72 if( i > 0 ) |
73 System.out.print('\t'); | 73 System.out.print('\t'); |
74 System.out.print( lua.toString(LuaElement.JAVA,args[i]) ); | 74 System.out.print( lua.toString(LuanElement.JAVA,args[i]) ); |
75 } | 75 } |
76 System.out.println(); | 76 System.out.println(); |
77 } | 77 } |
78 | 78 |
79 public static String type(Object obj) { | 79 public static String type(Object obj) { |
80 return Lua.type(obj); | 80 return Luan.type(obj); |
81 } | 81 } |
82 | 82 |
83 public static LuaFunction load(LuaState lua,String text,String sourceName) throws LuaException { | 83 public static LuanFunction load(LuanState lua,String text,String sourceName) throws LuanException { |
84 return LuaCompiler.compile(lua,new LuaSource(sourceName,text)); | 84 return LuanCompiler.compile(lua,new LuanSource(sourceName,text)); |
85 } | 85 } |
86 | 86 |
87 public static String readAll(Reader in) | 87 public static String readAll(Reader in) |
88 throws IOException | 88 throws IOException |
89 { | 89 { |
104 in.close(); | 104 in.close(); |
105 return s; | 105 return s; |
106 } | 106 } |
107 | 107 |
108 | 108 |
109 public static LuaFunction load_file(LuaState lua,String fileName) throws LuaException { | 109 public static LuanFunction load_file(LuanState lua,String fileName) throws LuanException { |
110 try { | 110 try { |
111 String src = fileName==null ? readAll(new InputStreamReader(System.in)) : read(new File(fileName)); | 111 String src = fileName==null ? readAll(new InputStreamReader(System.in)) : read(new File(fileName)); |
112 return load(lua,src,fileName); | 112 return load(lua,src,fileName); |
113 } catch(IOException e) { | 113 } catch(IOException e) { |
114 throw new LuaException(lua,LuaElement.JAVA,e); | 114 throw new LuanException(lua,LuanElement.JAVA,e); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 public static Object[] do_file(LuaState lua,String fileName) throws LuaException { | 118 public static Object[] do_file(LuanState lua,String fileName) throws LuanException { |
119 LuaFunction fn = load_file(lua,fileName); | 119 LuanFunction fn = load_file(lua,fileName); |
120 return lua.call(fn,LuaElement.JAVA,null); | 120 return lua.call(fn,LuanElement.JAVA,null); |
121 } | 121 } |
122 | 122 |
123 private static LuaFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 123 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { |
124 return new LuaFunction() { | 124 return new LuanFunction() { |
125 public Object[] call(LuaState lua,Object[] args) { | 125 public Object[] call(LuanState lua,Object[] args) { |
126 if( !iter.hasNext() ) | 126 if( !iter.hasNext() ) |
127 return LuaFunction.EMPTY_RTN; | 127 return LuanFunction.EMPTY_RTN; |
128 Map.Entry<Object,Object> entry = iter.next(); | 128 Map.Entry<Object,Object> entry = iter.next(); |
129 return new Object[]{entry.getKey(),entry.getValue()}; | 129 return new Object[]{entry.getKey(),entry.getValue()}; |
130 } | 130 } |
131 }; | 131 }; |
132 } | 132 } |
133 | 133 |
134 public static LuaFunction pairs(LuaTable t) { | 134 public static LuanFunction pairs(LuanTable t) { |
135 return pairs( t.iterator() ); | 135 return pairs( t.iterator() ); |
136 } | 136 } |
137 | 137 |
138 public static LuaFunction ipairs(LuaTable t) { | 138 public static LuanFunction ipairs(LuanTable t) { |
139 return pairs( t.listIterator() ); | 139 return pairs( t.listIterator() ); |
140 } | 140 } |
141 | 141 |
142 public static LuaTable get_metatable(LuaState lua,Object obj) { | 142 public static LuanTable get_metatable(LuanState lua,Object obj) { |
143 return lua.getMetatable(obj); | 143 return lua.getMetatable(obj); |
144 } | 144 } |
145 | 145 |
146 public static LuaTable set_metatable(LuaTable table,LuaTable metatable) { | 146 public static LuanTable set_metatable(LuanTable table,LuanTable metatable) { |
147 table.setMetatable(metatable); | 147 table.setMetatable(metatable); |
148 return table; | 148 return table; |
149 } | 149 } |
150 | 150 |
151 public static boolean raw_equal(Object v1,Object v2) { | 151 public static boolean raw_equal(Object v1,Object v2) { |
152 return v1 == v2 || v1 != null && v1.equals(v2); | 152 return v1 == v2 || v1 != null && v1.equals(v2); |
153 } | 153 } |
154 | 154 |
155 public static Object raw_get(LuaTable table,Object index) { | 155 public static Object raw_get(LuanTable table,Object index) { |
156 return table.get(index); | 156 return table.get(index); |
157 } | 157 } |
158 | 158 |
159 public static LuaTable raw_set(LuaTable table,Object index,Object value) { | 159 public static LuanTable raw_set(LuanTable table,Object index,Object value) { |
160 table.put(index,value); | 160 table.put(index,value); |
161 return table; | 161 return table; |
162 } | 162 } |
163 | 163 |
164 public static int raw_len(LuaState lua,Object v) throws LuaException { | 164 public static int raw_len(LuanState lua,Object v) throws LuanException { |
165 if( v instanceof String ) { | 165 if( v instanceof String ) { |
166 String s = (String)v; | 166 String s = (String)v; |
167 return s.length(); | 167 return s.length(); |
168 } | 168 } |
169 if( v instanceof LuaTable ) { | 169 if( v instanceof LuanTable ) { |
170 LuaTable t = (LuaTable)v; | 170 LuanTable t = (LuanTable)v; |
171 return t.length(); | 171 return t.length(); |
172 } | 172 } |
173 throw new LuaException( lua, LuaElement.JAVA, "bad argument #1 to 'raw_len' (table or string expected)" ); | 173 throw new LuanException( lua, LuanElement.JAVA, "bad argument #1 to 'raw_len' (table or string expected)" ); |
174 } | 174 } |
175 | 175 |
176 public static Number to_number(Object e,Integer base) { | 176 public static Number to_number(Object e,Integer base) { |
177 return Lua.toNumber(e,base); | 177 return Luan.toNumber(e,base); |
178 } | 178 } |
179 | 179 |
180 public static String to_string(LuaState lua,Object v) throws LuaException { | 180 public static String to_string(LuanState lua,Object v) throws LuanException { |
181 return lua.toString(LuaElement.JAVA,v); | 181 return lua.toString(LuanElement.JAVA,v); |
182 } | 182 } |
183 | 183 |
184 public static void error(LuaState lua,Object msg) throws LuaException { | 184 public static void error(LuanState lua,Object msg) throws LuanException { |
185 throw new LuaException(lua,LuaElement.JAVA,msg); | 185 throw new LuanException(lua,LuanElement.JAVA,msg); |
186 } | 186 } |
187 | 187 |
188 } | 188 } |