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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
6 import java.util.List;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
7 import java.util.ArrayList;
1335
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
8 import java.util.Map;
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
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
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
13 private LuanTable table;
1335
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
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
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
26 }
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
27
784
6a7c6879158d revert shallowClone()
Franklin Schmidt <fschmidt@gmail.com>
parents: 783
diff changeset
28 @Override public LuanException shallowClone() {
1282
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
29 return new LuanException(getMessage(),getCause());
689
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
30 }
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
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
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
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
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
35 clone.extra = (Map)cloner.clone(extra);
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
36 }
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
37
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
38 public void put(String key,Object value) throws LuanException {
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
39 if( table == null ) {
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
40 extra.put(key,value);
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
41 } else {
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
42 table.put(key,value);
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 782
diff changeset
47 if( table==null ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 782
diff changeset
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
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
50 table = (LuanTable)Boot.fn("new_error_table").call(this );
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
51 for( Object stupid : extra.entrySet() ) {
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
52 Map.Entry entry = (Map.Entry)stupid;
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
53 table.put( entry.getKey(), entry.getValue() );
e0cf0d108a77 major cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1333
diff changeset
54 }
1283
503bde9a7c80 add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1282
diff changeset
55 } catch(LuanException e) {
783
Franklin Schmidt <fschmidt@gmail.com>
parents: 782
diff changeset
56 throw new RuntimeException(e);
Franklin Schmidt <fschmidt@gmail.com>
parents: 782
diff changeset
57 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 782
diff changeset
58 }
689
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
59 return table;
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
60 }
51a134a8c89b fix LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents: 683
diff changeset
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
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
66 public String getJavaStackTraceString() {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
67 return getJavaStackTraceString(this);
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
68 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
69
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
70 private static String getJavaStackTraceString(Throwable th) {
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
71 StringWriter sw = new StringWriter();
682
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
72 th.printStackTrace(new PrintWriter(sw));
645
859c0dedc8b6 remove LuanSource
Franklin Schmidt <fschmidt@gmail.com>
parents: 578
diff changeset
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
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
77 List<StackTraceElement> list = new ArrayList<StackTraceElement>();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
78 for( int i=0; i<orig.length; i++ ) {
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
79 StackTraceElement ste = orig[i];
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
80 if( !ste.getClassName().startsWith("luan.impl.EXP") )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
81 continue;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
82 list.add(ste);
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
83 if( !ste.getMethodName().equals("doCall") )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
84 i++;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
85 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
86 return list;
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
87 }
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
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
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
111 StringBuilder sb = new StringBuilder();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
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
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
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
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
121 Throwable cause = getCause();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
122 if( cause != null )
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
123 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) );
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
124 return sb.toString();
0c334975d526 finish stack trace
Franklin Schmidt <fschmidt@gmail.com>
parents: 646
diff changeset
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 }