Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stubs for more unimplemented reflect methods #2574

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions src/reflect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,23 @@ func (k Kind) basicType() rawType {
return rawType(k << 1)
}

// Method represents a single method.
type Method struct {
// Name is the method name.
Name string

// PkgPath is the package path that qualifies a lower case (unexported)
// method name. It is empty for upper case (exported) method names.
// The combination of PkgPath and Name uniquely identifies a method
// in a method set.
// See https://golang.org/ref/spec#Uniqueness_of_identifiers
PkgPath string

Type Type // method type
Func Value // func with receiver as first argument
Index int // index for Type.Method
}
Comment on lines +129 to +144
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from Go source


// The following Type type has been copied almost entirely from
// https://github.com/golang/go/blob/go1.15/src/reflect/type.go#L27-L212.
// Some methods have been commented out as they haven't yet been implemented.
Expand Down Expand Up @@ -173,7 +190,7 @@ type Type interface {
//
// For an interface type, the returned Method's Type field gives the
// method signature, without a receiver, and the Func field is nil.
//MethodByName(string) (Method, bool)
MethodByName(string) (Method, bool)

// NumMethod returns the number of exported methods in the type's method set.
NumMethod() int
Expand All @@ -187,7 +204,7 @@ type Type interface {
// If the type was predeclared (string, error) or not defined (*T, struct{},
// []int, or A where A is an alias for a non-defined type), the package path
// will be the empty string.
//PkgPath() string
PkgPath() string

// Size returns the number of bytes needed to store
// a value of the given type; it is analogous to unsafe.Sizeof.
Expand Down Expand Up @@ -286,7 +303,7 @@ type Type interface {
// In returns the type of a function type's i'th input parameter.
// It panics if the type's Kind is not Func.
// It panics if i is not in the range [0, NumIn()).
//In(i int) Type
In(i int) Type

// Key returns a map type's key type.
// It panics if the type's Kind is not Map.
Expand All @@ -311,7 +328,7 @@ type Type interface {
// Out returns the type of a function type's i'th output parameter.
// It panics if the type's Kind is not Func.
// It panics if i is not in the range [0, NumOut()).
//Out(i int) Type
Out(i int) Type
}

// The typecode as used in an interface{}.
Expand Down Expand Up @@ -691,6 +708,22 @@ func (t rawType) Key() Type {
panic("unimplemented: (reflect.Type).Key()")
}

func (t rawType) In(i int) Type {
panic("unimplemented: (reflect.Type).In()")
}

func (t rawType) Out(i int) Type {
panic("unimplemented: (reflect.Type).Out()")
}

func (t rawType) MethodByName(name string) (Method, bool) {
panic("unimplemented: (reflect.Type).MethodByName()")
}

func (t rawType) PkgPath() string {
panic("unimplemented: (reflect.Type).PkgPath()")
}

// A StructField describes a single field in a struct.
type StructField struct {
// Name indicates the field name.
Expand Down Expand Up @@ -800,3 +833,7 @@ func (e *TypeError) Error() string {
func align(offset uintptr, alignment uintptr) uintptr {
return (offset + alignment - 1) &^ (alignment - 1)
}

func SliceOf(t Type) Type {
panic("unimplemented: reflect.SliceOf()")
}
48 changes: 30 additions & 18 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (v Value) IsNil() bool {
_, val := decomposeInterface(*(*interface{})(v.value))
return val == nil
default:
panic(&ValueError{"IsNil"})
panic(&ValueError{Method: "IsNil"})
}
}

Expand All @@ -144,7 +144,7 @@ func (v Value) Pointer() uintptr {
case Func:
panic("unimplemented: (reflect.Value).Pointer()")
default: // not implemented: Func
panic(&ValueError{"Pointer"})
panic(&ValueError{Method: "Pointer"})
}
}

Expand Down Expand Up @@ -187,7 +187,7 @@ func (v Value) Bool() bool {
return uintptr(v.value) != 0
}
default:
panic(&ValueError{"Bool"})
panic(&ValueError{Method: "Bool"})
}
}

Expand Down Expand Up @@ -224,7 +224,7 @@ func (v Value) Int() int64 {
return int64(int64(uintptr(v.value)))
}
default:
panic(&ValueError{"Int"})
panic(&ValueError{Method: "Int"})
}
}

Expand Down Expand Up @@ -267,7 +267,7 @@ func (v Value) Uint() uint64 {
return uint64(uintptr(v.value))
}
default:
panic(&ValueError{"Uint"})
panic(&ValueError{Method: "Uint"})
}
}

Expand All @@ -293,7 +293,7 @@ func (v Value) Float() float64 {
return *(*float64)(unsafe.Pointer(&v.value))
}
default:
panic(&ValueError{"Float"})
panic(&ValueError{Method: "Float"})
}
}

Expand All @@ -315,7 +315,7 @@ func (v Value) Complex() complex128 {
// architectures with 128-bit pointers, however.
return *(*complex128)(v.value)
default:
panic(&ValueError{"Complex"})
panic(&ValueError{Method: "Complex"})
}
}

Expand Down Expand Up @@ -360,7 +360,7 @@ func (v Value) Len() int {
case String:
return int((*stringHeader)(v.value).len)
default:
panic(&ValueError{"Len"})
panic(&ValueError{Method: "Len"})
}
}

Expand All @@ -378,7 +378,7 @@ func (v Value) Cap() int {
case Slice:
return int((*sliceHeader)(v.value).cap)
default:
panic(&ValueError{"Cap"})
panic(&ValueError{Method: "Cap"})
}
}

Expand Down Expand Up @@ -408,7 +408,7 @@ func (v Value) Elem() Value {
flags: v.flags &^ valueFlagIndirect,
}
default:
panic(&ValueError{"Elem"})
panic(&ValueError{Method: "Elem"})
}
}

Expand Down Expand Up @@ -552,7 +552,7 @@ func (v Value) Index(i int) Value {
value: unsafe.Pointer(value),
}
default:
panic(&ValueError{"Index"})
panic(&ValueError{Method: "Index"})
}
}

Expand Down Expand Up @@ -631,7 +631,7 @@ func (v Value) SetBool(x bool) {
case Bool:
*(*bool)(v.value) = x
default:
panic(&ValueError{"SetBool"})
panic(&ValueError{Method: "SetBool"})
}
}

Expand All @@ -649,7 +649,7 @@ func (v Value) SetInt(x int64) {
case Int64:
*(*int64)(v.value) = x
default:
panic(&ValueError{"SetInt"})
panic(&ValueError{Method: "SetInt"})
}
}

Expand All @@ -669,7 +669,7 @@ func (v Value) SetUint(x uint64) {
case Uintptr:
*(*uintptr)(v.value) = uintptr(x)
default:
panic(&ValueError{"SetUint"})
panic(&ValueError{Method: "SetUint"})
}
}

Expand All @@ -681,7 +681,7 @@ func (v Value) SetFloat(x float64) {
case Float64:
*(*float64)(v.value) = x
default:
panic(&ValueError{"SetFloat"})
panic(&ValueError{Method: "SetFloat"})
}
}

Expand All @@ -693,7 +693,7 @@ func (v Value) SetComplex(x complex128) {
case Complex128:
*(*complex128)(v.value) = x
default:
panic(&ValueError{"SetComplex"})
panic(&ValueError{Method: "SetComplex"})
}
}

Expand All @@ -703,7 +703,7 @@ func (v Value) SetString(x string) {
case String:
*(*string)(v.value) = x
default:
panic(&ValueError{"SetString"})
panic(&ValueError{Method: "SetString"})
}
}

Expand Down Expand Up @@ -788,10 +788,14 @@ type stringHeader struct {

type ValueError struct {
Method string
Kind Kind
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to match the ValueError from Go

}

func (e *ValueError) Error() string {
return "reflect: call of reflect.Value." + e.Method + " on invalid type"
if e.Kind == 0 {
return "reflect: call of " + e.Method + " on zero Value"
}
return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value"
Comment on lines +795 to +798
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from Go source

}

// Calls to this function are converted to LLVM intrinsic calls such as
Expand Down Expand Up @@ -865,3 +869,11 @@ func MakeMap(typ Type) Value {
func (v Value) Call(in []Value) []Value {
panic("unimplemented: (reflect.Value).Call()")
}

func (v Value) MethodByName(name string) Value {
panic("unimplemented: (reflect.Value).MethodByName()")
}

func NewAt(typ Type, p unsafe.Pointer) Value {
panic("unimplemented: reflect.New()")
}