2010-12-03 05:34:57 +01: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
|
|
|
|
|
|
|
|
// The Error interface identifies a run time error.
|
|
|
|
type Error interface {
|
2011-12-03 03:17:34 +01:00
|
|
|
error
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// RuntimeError is a no-op function but
|
|
|
|
// serves to distinguish types that are runtime
|
2011-12-03 03:17:34 +01:00
|
|
|
// errors from ordinary errors: a type is a
|
2010-12-03 05:34:57 +01:00
|
|
|
// runtime error if it has a RuntimeError method.
|
|
|
|
RuntimeError()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*TypeAssertionError) RuntimeError() {}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (e *TypeAssertionError) Error() string {
|
2010-12-03 05:34:57 +01:00
|
|
|
inter := e.interfaceString
|
|
|
|
if inter == "" {
|
|
|
|
inter = "interface"
|
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
if e.concreteString == "" {
|
2010-12-03 05:34:57 +01: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-03-02 17:38:43 +01:00
|
|
|
func NewTypeAssertionError(ps1, ps2, ps3 *string, pmeth *string, ret *interface{}) {
|
2010-12-03 05:34:57 +01: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-03-02 17:38:43 +01:00
|
|
|
*ret = &TypeAssertionError{s1, s2, s3, meth}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// An errorString represents a runtime error described by a single string.
|
|
|
|
type errorString string
|
|
|
|
|
|
|
|
func (e errorString) RuntimeError() {}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (e errorString) Error() string {
|
2010-12-03 05:34:57 +01:00
|
|
|
return "runtime error: " + string(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For calling from C.
|
|
|
|
func NewErrorString(s string, ret *interface{}) {
|
|
|
|
*ret = errorString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type stringer interface {
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func typestring(interface{}) string
|
|
|
|
|
|
|
|
// For calling from C.
|
|
|
|
// Prints an argument passed to panic.
|
|
|
|
// 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-12-03 03:17:34 +01:00
|
|
|
case error:
|
|
|
|
print(v.Error())
|
2010-12-03 05:34:57 +01:00
|
|
|
case int:
|
|
|
|
print(v)
|
|
|
|
case string:
|
|
|
|
print(v)
|
|
|
|
default:
|
|
|
|
print("(", typestring(i), ") ", i)
|
|
|
|
}
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
// called from generated code
|
|
|
|
func panicwrap(pkg, typ, meth string) {
|
|
|
|
panic("value method " + pkg + "." + typ + "." + meth + " called using nil *" + typ + " pointer")
|
|
|
|
}
|