Mercurial Hosting > luan
annotate src/luan/LuanException.java @ 1400:221eedb0f54e
fix inner class gc bug
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 13 Sep 2019 05:05:51 -0600 |
| parents | e0cf0d108a77 |
| children | e1a13e707bf3 |
| rev | line source |
|---|---|
|
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
1 package luan; |
|
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
2 |
|
200
9fb218211763
add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
3 import java.io.StringWriter; |
|
9fb218211763
add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
4 import java.io.PrintWriter; |
|
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
5 import java.io.PrintStream; |
| 682 | 6 import java.util.List; |
| 7 import java.util.ArrayList; | |
| 1335 | 8 import java.util.Map; |
| 9 import java.util.HashMap; | |
|
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
10 |
|
200
9fb218211763
add Package.block();
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
171
diff
changeset
|
11 |
|
781
fbbdd369a13a
rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
12 public final class LuanException extends Exception implements LuanCloneable { |
| 689 | 13 private LuanTable table; |
| 1335 | 14 private Map extra = new HashMap(); |
|
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
15 |
|
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
16 public LuanException(String msg,Throwable cause) { |
|
481
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
17 super(msg,cause); |
|
42
786699c78837
implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
40
diff
changeset
|
18 } |
|
786699c78837
implement try-catch
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
40
diff
changeset
|
19 |
|
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
20 public LuanException(String msg) { |
|
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
21 super(msg); |
|
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
22 } |
|
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
23 |
|
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
24 public LuanException(Throwable cause) { |
|
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
25 super(cause); |
| 689 | 26 } |
| 27 | |
|
784
6a7c6879158d
revert shallowClone()
Franklin Schmidt <fschmidt@gmail.com>
parents:
783
diff
changeset
|
28 @Override public LuanException shallowClone() { |
| 1282 | 29 return new LuanException(getMessage(),getCause()); |
| 689 | 30 } |
| 31 | |
|
781
fbbdd369a13a
rename DeepCloner to LuanCloner
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
32 @Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) { |
| 689 | 33 LuanException clone = (LuanException)dc; |
|
782
655280eab1e2
start limited cloning
Franklin Schmidt <fschmidt@gmail.com>
parents:
781
diff
changeset
|
34 clone.table = (LuanTable)cloner.clone(table); |
| 1335 | 35 clone.extra = (Map)cloner.clone(extra); |
| 36 } | |
| 37 | |
| 38 public void put(String key,Object value) throws LuanException { | |
| 39 if( table == null ) { | |
| 40 extra.put(key,value); | |
| 41 } else { | |
| 42 table.put(key,value); | |
| 43 } | |
|
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
44 } |
|
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
45 |
|
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
46 public LuanTable table(Luan luan) { |
| 783 | 47 if( table==null ) { |
| 48 try { | |
|
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1282
diff
changeset
|
49 LuanTable Boot = (LuanTable)luan.require("luan:Boot.luan"); |
| 1335 | 50 table = (LuanTable)Boot.fn("new_error_table").call(this ); |
| 51 for( Object stupid : extra.entrySet() ) { | |
| 52 Map.Entry entry = (Map.Entry)stupid; | |
| 53 table.put( entry.getKey(), entry.getValue() ); | |
| 54 } | |
|
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1282
diff
changeset
|
55 } catch(LuanException e) { |
| 783 | 56 throw new RuntimeException(e); |
| 57 } | |
| 58 } | |
| 689 | 59 return table; |
| 60 } | |
| 61 | |
|
481
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
62 public void throwThis() throws LuanException { |
|
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
63 throw this; |
|
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
64 } |
|
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
65 |
| 682 | 66 public String getJavaStackTraceString() { |
| 67 return getJavaStackTraceString(this); | |
| 68 } | |
| 69 | |
| 70 private static String getJavaStackTraceString(Throwable th) { | |
| 645 | 71 StringWriter sw = new StringWriter(); |
| 682 | 72 th.printStackTrace(new PrintWriter(sw)); |
| 645 | 73 return sw.toString(); |
|
37
8a57ebfdfd78
add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
2
diff
changeset
|
74 } |
|
481
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
75 |
|
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
76 public static List<StackTraceElement> justLuan(StackTraceElement[] orig) { |
| 682 | 77 List<StackTraceElement> list = new ArrayList<StackTraceElement>(); |
| 78 for( int i=0; i<orig.length; i++ ) { | |
| 79 StackTraceElement ste = orig[i]; | |
| 80 if( !ste.getClassName().startsWith("luan.impl.EXP") ) | |
| 81 continue; | |
| 82 list.add(ste); | |
| 83 if( !ste.getMethodName().equals("doCall") ) | |
| 84 i++; | |
| 85 } | |
| 86 return list; | |
| 87 } | |
| 88 | |
|
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
89 public static String toLuanString(StackTraceElement ste) { |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
90 int line = ste.getLineNumber(); |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
91 String method = ste.getMethodName(); |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
92 boolean hasMethod = !method.equals("doCall"); |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
93 if( hasMethod ) { |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
94 int i = method.indexOf('$'); |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
95 if( i != -1 ) { |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
96 int n = Integer.parseInt(method.substring(i+1)); |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
97 line -= n; |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
98 method = method.substring(0,i); |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
99 if( method.equals("_") ) |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
100 hasMethod = false; |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
101 } |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
102 } |
|
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
103 StringBuilder sb = new StringBuilder(); |
|
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
104 sb.append( ste.getFileName() ).append( " line " ).append( line ); |
|
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
105 if( hasMethod ) |
|
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
106 sb.append( " in function '" ).append( method.substring(1) ).append( "'" ); |
|
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
107 return sb.toString(); |
|
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
108 } |
|
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
109 |
|
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
110 private StringBuilder luanStackTrace() { |
| 682 | 111 StringBuilder sb = new StringBuilder(); |
| 112 sb.append( getMessage() ); | |
|
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
113 for( StackTraceElement ste : justLuan(getStackTrace()) ) { |
|
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
114 sb.append( "\n\t" ).append( toLuanString(ste) ); |
| 682 | 115 } |
|
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
116 return sb; |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
117 } |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
118 |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
119 public String getLuanStackTraceString() { |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
120 StringBuilder sb = luanStackTrace(); |
| 682 | 121 Throwable cause = getCause(); |
| 122 if( cause != null ) | |
| 123 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) ); | |
| 124 return sb.toString(); | |
| 125 } | |
|
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
126 |
|
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
127 @Override public void printStackTrace(PrintStream s) { |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
128 s.print("Luan: "); |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
129 s.println(luanStackTrace()); |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
130 s.print("Caused by: "); |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
131 super.printStackTrace(s); |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
132 } |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
133 |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
134 @Override public void printStackTrace(PrintWriter s) { |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
135 s.print("Luan: "); |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
136 s.println(luanStackTrace()); |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
137 s.print("Caused by: "); |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
138 super.printStackTrace(s); |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
139 } |
|
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
140 |
|
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
141 } |
