comparison src/luan/lib/BasicLib.java @ 40:e3624b7cd603

implement stack trace git-svn-id: https://luan-java.googlecode.com/svn/trunk@41 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 21 Dec 2012 10:45:54 +0000
parents e5bcb1eeafc1
children c53dc854e6cc
comparison
equal deleted inserted replaced
39:e5bcb1eeafc1 40:e3624b7cd603
13 import luan.LuaTable; 13 import luan.LuaTable;
14 import luan.LuaNumber; 14 import luan.LuaNumber;
15 import luan.LuaFunction; 15 import luan.LuaFunction;
16 import luan.LuaJavaFunction; 16 import luan.LuaJavaFunction;
17 import luan.LuaException; 17 import luan.LuaException;
18 import luan.LuaSource;
19 import luan.LuaElement;
18 import luan.interp.LuaCompiler; 20 import luan.interp.LuaCompiler;
19 21
20 22
21 public final class BasicLib { 23 public final class BasicLib {
22 24
24 LuaTable global = lua.global(); 26 LuaTable global = lua.global();
25 global.put( "_G", global ); 27 global.put( "_G", global );
26 add( global, "do_file", LuaState.class, String.class ); 28 add( global, "do_file", LuaState.class, String.class );
27 add( global, "get_metatable", LuaState.class, Object.class ); 29 add( global, "get_metatable", LuaState.class, Object.class );
28 add( global, "ipairs", LuaTable.class ); 30 add( global, "ipairs", LuaTable.class );
29 add( global, "load", LuaState.class, String.class ); 31 add( global, "load", LuaState.class, String.class, String.class );
30 add( global, "load_file", LuaState.class, String.class ); 32 add( global, "load_file", LuaState.class, String.class );
31 add( global, "pairs", LuaTable.class ); 33 add( global, "pairs", LuaTable.class );
32 add( global, "print", LuaState.class, new Object[0].getClass() ); 34 add( global, "print", LuaState.class, new Object[0].getClass() );
33 add( global, "raw_equal", Object.class, Object.class ); 35 add( global, "raw_equal", Object.class, Object.class );
34 add( global, "raw_get", LuaTable.class, Object.class ); 36 add( global, "raw_get", LuaTable.class, Object.class );
35 add( global, "raw_len", Object.class ); 37 add( global, "raw_len", LuaState.class, Object.class );
36 add( global, "raw_set", LuaTable.class, Object.class, Object.class ); 38 add( global, "raw_set", LuaTable.class, Object.class, Object.class );
37 add( global, "set_metatable", LuaTable.class, LuaTable.class ); 39 add( global, "set_metatable", LuaTable.class, LuaTable.class );
38 add( global, "to_number", Object.class, Integer.class ); 40 add( global, "to_number", Object.class, Integer.class );
39 add( global, "to_string", LuaState.class, Object.class ); 41 add( global, "to_string", LuaState.class, Object.class );
40 add( global, "type", Object.class ); 42 add( global, "type", Object.class );
67 69
68 public static void print(LuaState lua,Object... args) throws LuaException { 70 public static void print(LuaState lua,Object... args) throws LuaException {
69 for( int i=0; i<args.length; i++ ) { 71 for( int i=0; i<args.length; i++ ) {
70 if( i > 0 ) 72 if( i > 0 )
71 System.out.print('\t'); 73 System.out.print('\t');
72 System.out.print( lua.toString(args[i]) ); 74 System.out.print( lua.toString(LuaElement.JAVA,args[i]) );
73 } 75 }
74 System.out.println(); 76 System.out.println();
75 } 77 }
76 78
77 public static String type(Object obj) { 79 public static String type(Object obj) {
78 return Lua.type(obj); 80 return Lua.type(obj);
79 } 81 }
80 82
81 public static LuaFunction load(LuaState lua,String ld) throws LuaException { 83 public static LuaFunction load(LuaState lua,String text,String sourceName) throws LuaException {
82 return LuaCompiler.compile(lua,ld); 84 return LuaCompiler.compile(lua,new LuaSource(sourceName,text));
83 } 85 }
84 86
85 public static String readAll(Reader in) 87 public static String readAll(Reader in)
86 throws IOException 88 throws IOException
87 { 89 {
102 in.close(); 104 in.close();
103 return s; 105 return s;
104 } 106 }
105 107
106 108
107 public static LuaFunction load_file(LuaState lua,String fileName) throws LuaException,IOException { 109 public static LuaFunction load_file(LuaState lua,String fileName) throws LuaException {
108 String src = fileName==null ? readAll(new InputStreamReader(System.in)) : read(new File(fileName)); 110 try {
109 return load(lua,src); 111 String src = fileName==null ? readAll(new InputStreamReader(System.in)) : read(new File(fileName));
110 } 112 return load(lua,src,fileName);
111 113 } catch(IOException e) {
112 public static Object[] do_file(LuaState lua,String fileName) throws LuaException,IOException { 114 throw new LuaException(lua,LuaElement.JAVA,e);
113 return load_file(lua,fileName).call(lua); 115 }
116 }
117
118 public static Object[] do_file(LuaState lua,String fileName) throws LuaException {
119 LuaFunction fn = load_file(lua,fileName);
120 return lua.call(fn,LuaElement.JAVA,null);
114 } 121 }
115 122
116 private static class TableIter { 123 private static class TableIter {
117 private final Iterator<Map.Entry<Object,Object>> iter; 124 private final Iterator<Map.Entry<Object,Object>> iter;
118 125
195 public static LuaTable raw_set(LuaTable table,Object index,Object value) { 202 public static LuaTable raw_set(LuaTable table,Object index,Object value) {
196 table.put(index,value); 203 table.put(index,value);
197 return table; 204 return table;
198 } 205 }
199 206
200 public static int raw_len(Object v) throws LuaException { 207 public static int raw_len(LuaState lua,Object v) throws LuaException {
201 if( v instanceof String ) { 208 if( v instanceof String ) {
202 String s = (String)v; 209 String s = (String)v;
203 return s.length(); 210 return s.length();
204 } 211 }
205 if( v instanceof LuaTable ) { 212 if( v instanceof LuaTable ) {
206 LuaTable t = (LuaTable)v; 213 LuaTable t = (LuaTable)v;
207 return t.length(); 214 return t.length();
208 } 215 }
209 throw new LuaException( "bad argument #1 to 'raw_len' (table or string expected)" ); 216 throw new LuaException( lua, LuaElement.JAVA, "bad argument #1 to 'raw_len' (table or string expected)" );
210 } 217 }
211 218
212 public static LuaNumber to_number(Object e,Integer base) { 219 public static LuaNumber to_number(Object e,Integer base) {
213 return Lua.toNumber(e,base); 220 return Lua.toNumber(e,base);
214 } 221 }
215 222
216 public static String to_string(LuaState lua,Object v) throws LuaException { 223 public static String to_string(LuaState lua,Object v) throws LuaException {
217 return lua.toString(v); 224 return lua.toString(LuaElement.JAVA,v);
218 } 225 }
219 } 226 }