LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package runtime | 5 package runtime |
| 6 |
| 7 import "unsafe" |
6 | 8 |
7 // The Error interface identifies a run time error. | 9 // The Error interface identifies a run time error. |
8 type Error interface { | 10 type Error interface { |
9 error | 11 error |
10 | 12 |
11 // RuntimeError is a no-op function but | 13 // RuntimeError is a no-op function but |
12 // serves to distinguish types that are runtime | 14 // serves to distinguish types that are runtime |
13 // errors from ordinary errors: a type is a | 15 // errors from ordinary errors: a type is a |
14 // runtime error if it has a RuntimeError method. | 16 // runtime error if it has a RuntimeError method. |
15 RuntimeError() | 17 RuntimeError() |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 func (e errorString) Error() string { | 70 func (e errorString) Error() string { |
69 return "runtime error: " + string(e) | 71 return "runtime error: " + string(e) |
70 } | 72 } |
71 | 73 |
72 // For calling from C. | 74 // For calling from C. |
73 func newErrorString(s string, ret *interface{}) { | 75 func newErrorString(s string, ret *interface{}) { |
74 *ret = errorString(s) | 76 *ret = errorString(s) |
75 } | 77 } |
76 | 78 |
77 // An errorCString represents a runtime error described by a single C string. | 79 // An errorCString represents a runtime error described by a single C string. |
78 // Not "type errorCString uintptr" because of http://golang.org/issue/7084. | 80 // Not "type errorCString unsafe.Pointer" because of http://golang.org/issue/708
4. |
79 type errorCString struct{ cstr uintptr } | 81 // Not uintptr because we want to avoid an allocation if interfaces can't hold |
| 82 // uintptrs directly (and cstr _is_ a pointer). |
| 83 type errorCString struct{ cstr unsafe.Pointer } |
80 | 84 |
81 func (e errorCString) RuntimeError() {} | 85 func (e errorCString) RuntimeError() {} |
82 | 86 |
83 func (e errorCString) Error() string { | 87 func (e errorCString) Error() string { |
84 » return "runtime error: " + cstringToGo(e.cstr) | 88 » return "runtime error: " + cstringToGo(uintptr(e.cstr)) |
85 } | 89 } |
86 | 90 |
87 // For calling from C. | 91 // For calling from C. |
88 func newErrorCString(s uintptr, ret *interface{}) { | 92 func newErrorCString(s unsafe.Pointer, ret *interface{}) { |
89 *ret = errorCString{s} | 93 *ret = errorCString{s} |
90 } | 94 } |
91 | 95 |
92 type stringer interface { | 96 type stringer interface { |
93 String() string | 97 String() string |
94 } | 98 } |
95 | 99 |
96 func typestring(interface{}) string | 100 func typestring(interface{}) string |
97 | 101 |
98 // For calling from C. | 102 // For calling from C. |
(...skipping 14 matching lines...) Expand all Loading... |
113 print(v) | 117 print(v) |
114 default: | 118 default: |
115 print("(", typestring(i), ") ", i) | 119 print("(", typestring(i), ") ", i) |
116 } | 120 } |
117 } | 121 } |
118 | 122 |
119 // called from generated code | 123 // called from generated code |
120 func panicwrap(pkg, typ, meth string) { | 124 func panicwrap(pkg, typ, meth string) { |
121 panic("value method " + pkg + "." + typ + "." + meth + " called using ni
l *" + typ + " pointer") | 125 panic("value method " + pkg + "." + typ + "." + meth + " called using ni
l *" + typ + " pointer") |
122 } | 126 } |
LEFT | RIGHT |