2010-03-31 16:55:10 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
2014-08-23 21:01:59 -06:00
|
|
|
import "unsafe"
|
|
|
|
|
2010-03-31 16:55:10 -06:00
|
|
|
// The Error interface identifies a run time error.
|
|
|
|
type Error interface {
|
2011-11-01 19:48:27 -06:00
|
|
|
error
|
2010-04-01 23:31:27 -06:00
|
|
|
|
|
|
|
// RuntimeError is a no-op function but
|
2015-03-17 18:01:34 -06:00
|
|
|
// serves to distinguish types that are run time
|
2011-11-01 20:58:09 -06:00
|
|
|
// errors from ordinary errors: a type is a
|
2015-03-17 18:01:34 -06:00
|
|
|
// run time error if it has a RuntimeError method.
|
2010-04-01 23:31:27 -06:00
|
|
|
RuntimeError()
|
2010-03-31 16:55:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A TypeAssertionError explains a failed type assertion.
|
|
|
|
type TypeAssertionError struct {
|
|
|
|
interfaceString string
|
|
|
|
concreteString string
|
|
|
|
assertedString string
|
|
|
|
missingMethod string // one method needed by Interface, missing from Concrete
|
|
|
|
}
|
|
|
|
|
2010-04-01 23:31:27 -06:00
|
|
|
func (*TypeAssertionError) RuntimeError() {}
|
|
|
|
|
2011-11-01 19:48:27 -06:00
|
|
|
func (e *TypeAssertionError) Error() string {
|
2010-03-31 16:55:10 -06:00
|
|
|
inter := e.interfaceString
|
|
|
|
if inter == "" {
|
|
|
|
inter = "interface"
|
|
|
|
}
|
2012-02-12 21:26:20 -07:00
|
|
|
if e.concreteString == "" {
|
2010-03-31 16:55:10 -06:00
|
|
|
return "interface conversion: " + inter + " is nil, not " + e.assertedString
|
|
|
|
}
|
|
|
|
if e.missingMethod == "" {
|
|
|
|
return "interface conversion: " + inter + " is " + e.concreteString +
|
|
|
|
", not " + e.assertedString
|
|
|
|
}
|
|
|
|
return "interface conversion: " + e.concreteString + " is not " + e.assertedString +
|
|
|
|
": missing method " + e.missingMethod
|
|
|
|
}
|
|
|
|
|
|
|
|
// For calling from C.
|
2012-02-12 21:26:20 -07:00
|
|
|
func newTypeAssertionError(ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
|
2010-03-31 16:55:10 -06:00
|
|
|
var s1, s2, s3, meth string
|
|
|
|
|
|
|
|
if ps1 != nil {
|
|
|
|
s1 = *ps1
|
|
|
|
}
|
|
|
|
if ps2 != nil {
|
|
|
|
s2 = *ps2
|
|
|
|
}
|
|
|
|
if ps3 != nil {
|
|
|
|
s3 = *ps3
|
|
|
|
}
|
|
|
|
if pmeth != nil {
|
|
|
|
meth = *pmeth
|
|
|
|
}
|
2012-02-12 21:26:20 -07:00
|
|
|
*ret = &TypeAssertionError{s1, s2, s3, meth}
|
2010-03-31 16:55:10 -06:00
|
|
|
}
|
|
|
|
|
2010-04-01 23:31:27 -06:00
|
|
|
// An errorString represents a runtime error described by a single string.
|
|
|
|
type errorString string
|
|
|
|
|
|
|
|
func (e errorString) RuntimeError() {}
|
|
|
|
|
2011-11-01 19:48:27 -06:00
|
|
|
func (e errorString) Error() string {
|
2010-04-01 23:31:27 -06:00
|
|
|
return "runtime error: " + string(e)
|
|
|
|
}
|
|
|
|
|
2010-03-31 16:55:10 -06:00
|
|
|
type stringer interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
2014-08-28 08:46:59 -06:00
|
|
|
func typestring(x interface{}) string {
|
|
|
|
e := (*eface)(unsafe.Pointer(&x))
|
|
|
|
return *e._type._string
|
|
|
|
}
|
2010-10-22 15:04:32 -06:00
|
|
|
|
2010-03-31 16:55:10 -06:00
|
|
|
// For calling from C.
|
2010-04-01 23:31:27 -06:00
|
|
|
// Prints an argument passed to panic.
|
2010-03-31 16:55:10 -06:00
|
|
|
// There's room for arbitrary complexity here, but we keep it
|
|
|
|
// simple and handle just a few important cases: int, string, and Stringer.
|
|
|
|
func printany(i interface{}) {
|
|
|
|
switch v := i.(type) {
|
|
|
|
case nil:
|
|
|
|
print("nil")
|
|
|
|
case stringer:
|
|
|
|
print(v.String())
|
2011-11-01 19:48:27 -06:00
|
|
|
case error:
|
|
|
|
print(v.Error())
|
2010-03-31 16:55:10 -06:00
|
|
|
case int:
|
|
|
|
print(v)
|
|
|
|
case string:
|
|
|
|
print(v)
|
|
|
|
default:
|
2010-10-22 15:04:32 -06:00
|
|
|
print("(", typestring(i), ") ", i)
|
2010-03-31 16:55:10 -06:00
|
|
|
}
|
|
|
|
}
|
2011-06-17 13:23:27 -06:00
|
|
|
|
|
|
|
// called from generated code
|
|
|
|
func panicwrap(pkg, typ, meth string) {
|
|
|
|
panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")
|
|
|
|
}
|