Mercurial Hosting > luan
comparison src/luan/lib/BasicLib.java @ 49:8ede219cd111
add WebShell
git-svn-id: https://luan-java.googlecode.com/svn/trunk@50 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 28 Dec 2012 19:35:04 +0000 |
parents | 64ecb7a3aad7 |
children | 177cfdc2bdb3 |
comparison
equal
deleted
inserted
replaced
48:64ecb7a3aad7 | 49:8ede219cd111 |
---|---|
19 import luan.interp.LuanCompiler; | 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(LuanState lua) { | 24 public static void register(LuanState luan) { |
25 LuanTable global = lua.global(); | 25 LuanTable global = luan.global(); |
26 global.put( "_G", global ); | 26 global.put( "_G", global ); |
27 add( global, "do_file", LuanState.class, String.class ); | 27 add( global, "do_file", LuanState.class, String.class ); |
28 add( global, "error", LuanState.class, Object.class ); | 28 add( global, "error", LuanState.class, Object.class ); |
29 add( global, "get_metatable", LuanState.class, Object.class ); | 29 add( global, "get_metatable", LuanState.class, Object.class ); |
30 add( global, "ipairs", LuanTable.class ); | 30 add( global, "ipairs", LuanTable.class ); |
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(LuanState lua) { | 56 public static void make_standard(LuanState luan) { |
57 LuanTable global = lua.global(); | 57 LuanTable global = luan.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(LuanState lua,Object... args) throws LuanException { | 70 public static void print(LuanState luan,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 luan.out.print('\t'); |
74 System.out.print( lua.toString(LuanElement.JAVA,args[i]) ); | 74 luan.out.print( luan.toString(LuanElement.JAVA,args[i]) ); |
75 } | 75 } |
76 System.out.println(); | 76 luan.out.println(); |
77 } | 77 } |
78 | 78 |
79 public static String type(Object obj) { | 79 public static String type(Object obj) { |
80 return Luan.type(obj); | 80 return Luan.type(obj); |
81 } | 81 } |
82 | 82 |
83 public static LuanFunction load(LuanState lua,String text,String sourceName) throws LuanException { | 83 public static LuanFunction load(LuanState luan,String text,String sourceName) throws LuanException { |
84 return LuanCompiler.compile(lua,new LuanSource(sourceName,text)); | 84 return LuanCompiler.compile(luan,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 LuanFunction load_file(LuanState lua,String fileName) throws LuanException { | 109 public static LuanFunction load_file(LuanState luan,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(luan,src,fileName); |
113 } catch(IOException e) { | 113 } catch(IOException e) { |
114 throw new LuanException(lua,LuanElement.JAVA,e); | 114 throw new LuanException(luan,LuanElement.JAVA,e); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 public static Object[] do_file(LuanState lua,String fileName) throws LuanException { | 118 public static Object[] do_file(LuanState luan,String fileName) throws LuanException { |
119 LuanFunction fn = load_file(lua,fileName); | 119 LuanFunction fn = load_file(luan,fileName); |
120 return lua.call(fn,LuanElement.JAVA,null); | 120 return luan.call(fn,LuanElement.JAVA,null); |
121 } | 121 } |
122 | 122 |
123 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { | 123 private static LuanFunction pairs(final Iterator<Map.Entry<Object,Object>> iter) { |
124 return new LuanFunction() { | 124 return new LuanFunction() { |
125 public Object[] call(LuanState lua,Object[] args) { | 125 public Object[] call(LuanState luan,Object[] args) { |
126 if( !iter.hasNext() ) | 126 if( !iter.hasNext() ) |
127 return LuanFunction.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 } |
137 | 137 |
138 public static LuanFunction ipairs(LuanTable 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 LuanTable get_metatable(LuanState lua,Object obj) { | 142 public static LuanTable get_metatable(LuanState luan,Object obj) { |
143 return lua.getMetatable(obj); | 143 return luan.getMetatable(obj); |
144 } | 144 } |
145 | 145 |
146 public static LuanTable set_metatable(LuanTable table,LuanTable 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; |
159 public static LuanTable raw_set(LuanTable 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(LuanState lua,Object v) throws LuanException { | 164 public static int raw_len(LuanState luan,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 LuanTable ) { | 169 if( v instanceof LuanTable ) { |
170 LuanTable t = (LuanTable)v; | 170 LuanTable t = (LuanTable)v; |
171 return t.length(); | 171 return t.length(); |
172 } | 172 } |
173 throw new LuanException( lua, LuanElement.JAVA, "bad argument #1 to 'raw_len' (table or string expected)" ); | 173 throw new LuanException( luan, 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 Luan.toNumber(e,base); | 177 return Luan.toNumber(e,base); |
178 } | 178 } |
179 | 179 |
180 public static String to_string(LuanState lua,Object v) throws LuanException { | 180 public static String to_string(LuanState luan,Object v) throws LuanException { |
181 return lua.toString(LuanElement.JAVA,v); | 181 return luan.toString(LuanElement.JAVA,v); |
182 } | 182 } |
183 | 183 |
184 public static void error(LuanState lua,Object msg) throws LuanException { | 184 public static void error(LuanState luan,Object msg) throws LuanException { |
185 throw new LuanException(lua,LuanElement.JAVA,msg); | 185 throw new LuanException(luan,LuanElement.JAVA,msg); |
186 } | 186 } |
187 | 187 |
188 } | 188 } |