comparison core/src/luan/LuanException.java @ 531:f99c79b0b426

change LuanException.getFullMessage() to not require LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 27 May 2015 22:01:40 -0600
parents 8dcf9e12446b
children f1601a4ce1aa
comparison
equal deleted inserted replaced
530:7366101be4c4 531:f99c79b0b426
11 super(msg,cause); 11 super(msg,cause);
12 } 12 }
13 13
14 LuanException(LuanBit bit,Object msg) throws LuanException { 14 LuanException(LuanBit bit,Object msg) throws LuanException {
15 this( bit.luan.toString(msg), msg instanceof Throwable ? (Throwable)msg : null ); 15 this( bit.luan.toString(msg), msg instanceof Throwable ? (Throwable)msg : null );
16 table.rawPut("java",this); 16 table.rawPut( "java", this );
17 table.rawPut("message",msg); 17 table.rawPut( "message", msg );
18 table.rawPut( "message_string", bit.luan.toString(msg) );
18 for( StackTraceElement ste : bit.stackTrace() ) { 19 for( StackTraceElement ste : bit.stackTrace() ) {
19 LuanTable tbl = new LuanTable(); 20 LuanTable tbl = new LuanTable();
20 tbl.rawPut( "source", ste.call.source.name ); 21 tbl.rawPut( "source", ste.call.source.name );
21 tbl.rawPut( "line", ste.call.lineNumber() ); 22 tbl.rawPut( "line", ste.call.lineNumber() );
22 tbl.rawPut( "call_to", ste.fnName ); 23 tbl.rawPut( "call_to", ste.fnName );
27 try { 28 try {
28 table.rawPut( "throw", new LuanJavaFunction( 29 table.rawPut( "throw", new LuanJavaFunction(
29 LuanException.class.getMethod( "throwThis" ), this 30 LuanException.class.getMethod( "throwThis" ), this
30 ) ); 31 ) );
31 metatable.rawPut( "__to_string", new LuanJavaFunction( 32 metatable.rawPut( "__to_string", new LuanJavaFunction(
32 LuanException.class.getMethod( "__to_string", LuanState.class, LuanTable.class ), null 33 LuanException.class.getMethod( "__to_string", LuanTable.class ), null
33 ) ); 34 ) );
34 } catch(NoSuchMethodException e) { 35 } catch(NoSuchMethodException e) {
35 throw new RuntimeException(e); 36 throw new RuntimeException(e);
36 } 37 }
37 } 38 }
51 52
52 public void throwThis() throws LuanException { 53 public void throwThis() throws LuanException {
53 throw this; 54 throw this;
54 } 55 }
55 56
56 public String getFullMessage(LuanState luan) { 57 public String getFullMessage() {
57 try { 58 return __to_string(table);
58 return __to_string(luan,table);
59 } catch(LuanException e) {
60 throw new RuntimeException(e);
61 }
62 } 59 }
63 60
64 public static String __to_string(LuanState luan,LuanTable table) throws LuanException { 61 public static String __to_string(LuanTable table) {
65 StringBuilder buf = new StringBuilder(); 62 StringBuilder buf = new StringBuilder();
66 63
67 Object msg = table.rawGet("message"); 64 Object msg = table.rawGet("message");
68 buf.append( luan.toString(msg) ); 65 String msgStr = (String)table.rawGet("message_string");
66 buf.append( msgStr );
69 67
70 for( int i = table.rawLength(); i>=1; i-- ) { 68 for( int i = table.rawLength(); i>=1; i-- ) {
71 LuanTable tbl = (LuanTable)table.rawGet(i); 69 LuanTable tbl = (LuanTable)table.rawGet(i);
72 buf.append( "\n\t" ).append( tbl.rawGet("source") ).append( " line " ).append( tbl.rawGet("line") ); 70 buf.append( "\n\t" ).append( tbl.rawGet("source") ).append( " line " ).append( tbl.rawGet("line") );
73 Object callTo = tbl.rawGet("call_to"); 71 Object callTo = tbl.rawGet("call_to");