1
2
3
4
5 package runtime
6
7
8 type Error interface {
9 String() string
10
11
12
13
14
15 RuntimeError()
16 }
17
18
19 type TypeAssertionError struct {
20 interfaceType Type
21 concreteType Type
22 assertedType Type
23 interfaceString string
24 concreteString string
25 assertedString string
26 missingMethod string
27 }
28
29 func (*TypeAssertionError) RuntimeError() {}
30
31 func (e *TypeAssertionError) String() string {
32 inter := e.interfaceString
33 if inter == "" {
34 inter = "interface"
35 }
36 if e.concreteType == nil {
37 return "interface conversion: " + inter + " is nil, not " + e.assertedString
38 }
39 if e.missingMethod == "" {
40 return "interface conversion: " + inter + " is " + e.concreteString +
41 ", not " + e.assertedString
42 }
43 return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
44 ": missing method " + e.missingMethod
45 }
46
47
48
49 func (e *TypeAssertionError) Concrete() Type {
50 return e.concreteType
51 }
52
53
54 func (e *TypeAssertionError) Asserted() Type {
55 return e.assertedType
56 }
57
58
59
60
61
62
63 func (e *TypeAssertionError) MissingMethod() string {
64 return e.missingMethod
65 }
66
67
68 func newTypeAssertionError(pt1, pt2, pt3 *Type, ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
69 var t1, t2, t3 Type
70 var s1, s2, s3, meth string
71
72 if pt1 != nil {
73 t1 = *pt1
74 }
75 if pt2 != nil {
76 t2 = *pt2
77 }
78 if pt3 != nil {
79 t3 = *pt3
80 }
81 if ps1 != nil {
82 s1 = *ps1
83 }
84 if ps2 != nil {
85 s2 = *ps2
86 }
87 if ps3 != nil {
88 s3 = *ps3
89 }
90 if pmeth != nil {
91 meth = *pmeth
92 }
93 *ret = &TypeAssertionError{t1, t2, t3, s1, s2, s3, meth}
94 }
95
96
97 type errorString string
98
99 func (e errorString) RuntimeError() {}
100
101 func (e errorString) String() string {
102 return "runtime error: " + string(e)
103 }
104
105
106 func newErrorString(s string, ret *interface{}) {
107 *ret = errorString(s)
108 }
109
110 type stringer interface {
111 String() string
112 }
113
114 func typestring(interface{}) string
115
116
117
118
119
120 func printany(i interface{}) {
121 switch v := i.(type) {
122 case nil:
123 print("nil")
124 case stringer:
125 print(v.String())
126 case int:
127 print(v)
128 case string:
129 print(v)
130 default:
131 print("(", typestring(i), ") ", i)
132 }
133 }
134
135
136 func panicwrap(pkg, typ, meth string) {
137 panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")
138 }