Mercurial Hosting > luan
annotate src/luan/LuanException.java @ 1561:e1a13e707bf3
start immutable
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 05 Nov 2020 20:24:09 -0700 |
parents | 221eedb0f54e |
children | b89212fd04b5 |
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 | |
1561 | 38 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException { |
39 immutabler.makeImmutable(table); | |
40 immutabler.makeImmutable(extra); | |
41 } | |
42 | |
1335 | 43 public void put(String key,Object value) throws LuanException { |
44 if( table == null ) { | |
45 extra.put(key,value); | |
46 } else { | |
47 table.put(key,value); | |
48 } | |
646
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
49 } |
cdc70de628b5
simplify LuanException
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
50 |
1333
25746915a241
merge Luan and LuanState
Franklin Schmidt <fschmidt@gmail.com>
parents:
1330
diff
changeset
|
51 public LuanTable table(Luan luan) { |
783 | 52 if( table==null ) { |
53 try { | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1282
diff
changeset
|
54 LuanTable Boot = (LuanTable)luan.require("luan:Boot.luan"); |
1335 | 55 table = (LuanTable)Boot.fn("new_error_table").call(this ); |
56 for( Object stupid : extra.entrySet() ) { | |
57 Map.Entry entry = (Map.Entry)stupid; | |
58 table.put( entry.getKey(), entry.getValue() ); | |
59 } | |
1283
503bde9a7c80
add luan.require() and table.call()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1282
diff
changeset
|
60 } catch(LuanException e) { |
783 | 61 throw new RuntimeException(e); |
62 } | |
63 } | |
689 | 64 return table; |
65 } | |
66 | |
481
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
67 public void throwThis() throws LuanException { |
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
68 throw this; |
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
69 } |
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
70 |
682 | 71 public String getJavaStackTraceString() { |
72 return getJavaStackTraceString(this); | |
73 } | |
74 | |
75 private static String getJavaStackTraceString(Throwable th) { | |
645 | 76 StringWriter sw = new StringWriter(); |
682 | 77 th.printStackTrace(new PrintWriter(sw)); |
645 | 78 return sw.toString(); |
37
8a57ebfdfd78
add JavaLib
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
2
diff
changeset
|
79 } |
481
5d4a78c93383
luan errors are now tables
Franklin Schmidt <fschmidt@gmail.com>
parents:
200
diff
changeset
|
80 |
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
81 public static List<StackTraceElement> justLuan(StackTraceElement[] orig) { |
682 | 82 List<StackTraceElement> list = new ArrayList<StackTraceElement>(); |
83 for( int i=0; i<orig.length; i++ ) { | |
84 StackTraceElement ste = orig[i]; | |
85 if( !ste.getClassName().startsWith("luan.impl.EXP") ) | |
86 continue; | |
87 list.add(ste); | |
88 if( !ste.getMethodName().equals("doCall") ) | |
89 i++; | |
90 } | |
91 return list; | |
92 } | |
93 | |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
94 public static String toLuanString(StackTraceElement ste) { |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
95 int line = ste.getLineNumber(); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
96 String method = ste.getMethodName(); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
97 boolean hasMethod = !method.equals("doCall"); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
98 if( hasMethod ) { |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
99 int i = method.indexOf('$'); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
100 if( i != -1 ) { |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
101 int n = Integer.parseInt(method.substring(i+1)); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
102 line -= n; |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
103 method = method.substring(0,i); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
104 if( method.equals("_") ) |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
105 hasMethod = false; |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
106 } |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
107 } |
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
108 StringBuilder sb = new StringBuilder(); |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
109 sb.append( ste.getFileName() ).append( " line " ).append( line ); |
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
110 if( hasMethod ) |
772
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
111 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
|
112 return sb.toString(); |
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
113 } |
bffbef96ca6d
add public static methods to LuanException for building luan stack traces
Franklin Schmidt <fschmidt@gmail.com>
parents:
693
diff
changeset
|
114 |
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
115 private StringBuilder luanStackTrace() { |
682 | 116 StringBuilder sb = new StringBuilder(); |
117 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
|
118 for( StackTraceElement ste : justLuan(getStackTrace()) ) { |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
119 sb.append( "\n\t" ).append( toLuanString(ste) ); |
682 | 120 } |
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
121 return sb; |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
122 } |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
123 |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
124 public String getLuanStackTraceString() { |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
125 StringBuilder sb = luanStackTrace(); |
682 | 126 Throwable cause = getCause(); |
127 if( cause != null ) | |
128 sb.append( "\nCaused by: " ).append( getJavaStackTraceString(cause) ); | |
129 return sb.toString(); | |
130 } | |
1400
221eedb0f54e
fix inner class gc bug
Franklin Schmidt <fschmidt@gmail.com>
parents:
1335
diff
changeset
|
131 |
1125
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
132 @Override public void printStackTrace(PrintStream s) { |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
133 s.print("Luan: "); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
134 s.println(luanStackTrace()); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
135 s.print("Caused by: "); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
136 super.printStackTrace(s); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
137 } |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
138 |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
139 @Override public void printStackTrace(PrintWriter s) { |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
140 s.print("Luan: "); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
141 s.println(luanStackTrace()); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
142 s.print("Caused by: "); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
143 super.printStackTrace(s); |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
144 } |
442abdfff437
include luan stack in LuanException.printStackTrace()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1122
diff
changeset
|
145 |
1
2df768b926aa
start Luan
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
146 } |