Skip to content

Commit cfed3f0

Browse files
kyleconroyniaow
authored andcommitted
fix: Add stubs for more missing reflect methods
With these methods stubbed out, the text/template package can be imported. These changes also allow code generated by protoc to compile.
1 parent d75e142 commit cfed3f0

File tree

2 files changed

+117
-27
lines changed

2 files changed

+117
-27
lines changed

src/reflect/type.go

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,35 @@ func (k Kind) basicType() rawType {
126126
return rawType(k << 1)
127127
}
128128

129+
// Copied from reflect/type.go
130+
// https://go.dev/src/reflect/type.go?#L348
131+
132+
// ChanDir represents a channel type's direction.
133+
type ChanDir int
134+
135+
const (
136+
RecvDir ChanDir = 1 << iota // <-chan
137+
SendDir // chan<-
138+
BothDir = RecvDir | SendDir // chan
139+
)
140+
141+
// Method represents a single method.
142+
type Method struct {
143+
// Name is the method name.
144+
Name string
145+
146+
// PkgPath is the package path that qualifies a lower case (unexported)
147+
// method name. It is empty for upper case (exported) method names.
148+
// The combination of PkgPath and Name uniquely identifies a method
149+
// in a method set.
150+
// See https://golang.org/ref/spec#Uniqueness_of_identifiers
151+
PkgPath string
152+
153+
Type Type // method type
154+
Func Value // func with receiver as first argument
155+
Index int // index for Type.Method
156+
}
157+
129158
// The following Type type has been copied almost entirely from
130159
// https://github.com/golang/go/blob/go1.15/src/reflect/type.go#L27-L212.
131160
// Some methods have been commented out as they haven't yet been implemented.
@@ -173,7 +202,7 @@ type Type interface {
173202
//
174203
// For an interface type, the returned Method's Type field gives the
175204
// method signature, without a receiver, and the Func field is nil.
176-
//MethodByName(string) (Method, bool)
205+
MethodByName(string) (Method, bool)
177206

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

192221
// Size returns the number of bytes needed to store
193222
// a value of the given type; it is analogous to unsafe.Sizeof.
@@ -234,7 +263,7 @@ type Type interface {
234263

235264
// ChanDir returns a channel type's direction.
236265
// It panics if the type's Kind is not Chan.
237-
//ChanDir() ChanDir
266+
ChanDir() ChanDir
238267

239268
// IsVariadic reports whether a function type's final input parameter
240269
// is a "..." parameter. If so, t.In(t.NumIn() - 1) returns the parameter's
@@ -248,7 +277,7 @@ type Type interface {
248277
// t.IsVariadic() == true
249278
//
250279
// IsVariadic panics if the type's Kind is not Func.
251-
//IsVariadic() bool
280+
IsVariadic() bool
252281

253282
// Elem returns a type's element type.
254283
// It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
@@ -267,7 +296,7 @@ type Type interface {
267296

268297
// FieldByName returns the struct field with the given name
269298
// and a boolean indicating if the field was found.
270-
//FieldByName(name string) (StructField, bool)
299+
FieldByName(name string) (StructField, bool)
271300

272301
// FieldByNameFunc returns the struct field with a name
273302
// that satisfies the match function and a boolean indicating if
@@ -286,7 +315,7 @@ type Type interface {
286315
// In returns the type of a function type's i'th input parameter.
287316
// It panics if the type's Kind is not Func.
288317
// It panics if i is not in the range [0, NumIn()).
289-
//In(i int) Type
318+
In(i int) Type
290319

291320
// Key returns a map type's key type.
292321
// It panics if the type's Kind is not Map.
@@ -302,16 +331,16 @@ type Type interface {
302331

303332
// NumIn returns a function type's input parameter count.
304333
// It panics if the type's Kind is not Func.
305-
//NumIn() int
334+
NumIn() int
306335

307336
// NumOut returns a function type's output parameter count.
308337
// It panics if the type's Kind is not Func.
309-
//NumOut() int
338+
NumOut() int
310339

311340
// Out returns the type of a function type's i'th output parameter.
312341
// It panics if the type's Kind is not Func.
313342
// It panics if i is not in the range [0, NumOut()).
314-
//Out(i int) Type
343+
Out(i int) Type
315344
}
316345

317346
// The typecode as used in an interface{}.
@@ -675,10 +704,26 @@ func (t rawType) Comparable() bool {
675704
}
676705
}
677706

707+
func (t rawType) ChanDir() ChanDir {
708+
panic("unimplemented: (reflect.Type).ChanDir()")
709+
}
710+
678711
func (t rawType) ConvertibleTo(u Type) bool {
679712
panic("unimplemented: (reflect.Type).ConvertibleTo()")
680713
}
681714

715+
func (t rawType) IsVariadic() bool {
716+
panic("unimplemented: (reflect.Type).IsVariadic()")
717+
}
718+
719+
func (t rawType) NumIn() int {
720+
panic("unimplemented: (reflect.Type).NumIn()")
721+
}
722+
723+
func (t rawType) NumOut() int {
724+
panic("unimplemented: (reflect.Type).NumOut()")
725+
}
726+
682727
func (t rawType) NumMethod() int {
683728
panic("unimplemented: (reflect.Type).NumMethod()")
684729
}
@@ -691,6 +736,26 @@ func (t rawType) Key() Type {
691736
panic("unimplemented: (reflect.Type).Key()")
692737
}
693738

739+
func (t rawType) In(i int) Type {
740+
panic("unimplemented: (reflect.Type).In()")
741+
}
742+
743+
func (t rawType) Out(i int) Type {
744+
panic("unimplemented: (reflect.Type).Out()")
745+
}
746+
747+
func (t rawType) MethodByName(name string) (Method, bool) {
748+
panic("unimplemented: (reflect.Type).MethodByName()")
749+
}
750+
751+
func (t rawType) PkgPath() string {
752+
panic("unimplemented: (reflect.Type).PkgPath()")
753+
}
754+
755+
func (t rawType) FieldByName(name string) (StructField, bool) {
756+
panic("unimplemented: (reflect.Type).FieldByName()")
757+
}
758+
694759
// A StructField describes a single field in a struct.
695760
type StructField struct {
696761
// Name indicates the field name.
@@ -704,6 +769,7 @@ type StructField struct {
704769
Tag StructTag // field tag string
705770
Anonymous bool
706771
Offset uintptr
772+
Index []int // index sequence for Type.FieldByIndex
707773
}
708774

709775
// IsExported reports whether the field is exported.
@@ -800,3 +866,7 @@ func (e *TypeError) Error() string {
800866
func align(offset uintptr, alignment uintptr) uintptr {
801867
return (offset + alignment - 1) &^ (alignment - 1)
802868
}
869+
870+
func SliceOf(t Type) Type {
871+
panic("unimplemented: reflect.SliceOf()")
872+
}

src/reflect/value.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (v Value) IsNil() bool {
128128
_, val := decomposeInterface(*(*interface{})(v.value))
129129
return val == nil
130130
default:
131-
panic(&ValueError{"IsNil"})
131+
panic(&ValueError{Method: "IsNil"})
132132
}
133133
}
134134

@@ -144,7 +144,7 @@ func (v Value) Pointer() uintptr {
144144
case Func:
145145
panic("unimplemented: (reflect.Value).Pointer()")
146146
default: // not implemented: Func
147-
panic(&ValueError{"Pointer"})
147+
panic(&ValueError{Method: "Pointer"})
148148
}
149149
}
150150

@@ -187,7 +187,7 @@ func (v Value) Bool() bool {
187187
return uintptr(v.value) != 0
188188
}
189189
default:
190-
panic(&ValueError{"Bool"})
190+
panic(&ValueError{Method: "Bool"})
191191
}
192192
}
193193

@@ -224,7 +224,7 @@ func (v Value) Int() int64 {
224224
return int64(int64(uintptr(v.value)))
225225
}
226226
default:
227-
panic(&ValueError{"Int"})
227+
panic(&ValueError{Method: "Int"})
228228
}
229229
}
230230

@@ -267,7 +267,7 @@ func (v Value) Uint() uint64 {
267267
return uint64(uintptr(v.value))
268268
}
269269
default:
270-
panic(&ValueError{"Uint"})
270+
panic(&ValueError{Method: "Uint"})
271271
}
272272
}
273273

@@ -293,7 +293,7 @@ func (v Value) Float() float64 {
293293
return *(*float64)(unsafe.Pointer(&v.value))
294294
}
295295
default:
296-
panic(&ValueError{"Float"})
296+
panic(&ValueError{Method: "Float"})
297297
}
298298
}
299299

@@ -315,7 +315,7 @@ func (v Value) Complex() complex128 {
315315
// architectures with 128-bit pointers, however.
316316
return *(*complex128)(v.value)
317317
default:
318-
panic(&ValueError{"Complex"})
318+
panic(&ValueError{Method: "Complex"})
319319
}
320320
}
321321

@@ -339,6 +339,10 @@ func (v Value) Slice(i, j int) Value {
339339
panic("unimplemented: (reflect.Value).Slice()")
340340
}
341341

342+
func (v Value) Slice3(i, j, k int) Value {
343+
panic("unimplemented: (reflect.Value).Slice3()")
344+
}
345+
342346
//go:linkname maplen runtime.hashmapLenUnsafePointer
343347
func maplen(p unsafe.Pointer) int
344348

@@ -360,7 +364,7 @@ func (v Value) Len() int {
360364
case String:
361365
return int((*stringHeader)(v.value).len)
362366
default:
363-
panic(&ValueError{"Len"})
367+
panic(&ValueError{Method: "Len"})
364368
}
365369
}
366370

@@ -378,7 +382,7 @@ func (v Value) Cap() int {
378382
case Slice:
379383
return int((*sliceHeader)(v.value).cap)
380384
default:
381-
panic(&ValueError{"Cap"})
385+
panic(&ValueError{Method: "Cap"})
382386
}
383387
}
384388

@@ -408,7 +412,7 @@ func (v Value) Elem() Value {
408412
flags: v.flags &^ valueFlagIndirect,
409413
}
410414
default:
411-
panic(&ValueError{"Elem"})
415+
panic(&ValueError{Method: "Elem"})
412416
}
413417
}
414418

@@ -552,7 +556,7 @@ func (v Value) Index(i int) Value {
552556
value: unsafe.Pointer(value),
553557
}
554558
default:
555-
panic(&ValueError{"Index"})
559+
panic(&ValueError{Method: "Index"})
556560
}
557561
}
558562

@@ -631,7 +635,7 @@ func (v Value) SetBool(x bool) {
631635
case Bool:
632636
*(*bool)(v.value) = x
633637
default:
634-
panic(&ValueError{"SetBool"})
638+
panic(&ValueError{Method: "SetBool"})
635639
}
636640
}
637641

@@ -649,7 +653,7 @@ func (v Value) SetInt(x int64) {
649653
case Int64:
650654
*(*int64)(v.value) = x
651655
default:
652-
panic(&ValueError{"SetInt"})
656+
panic(&ValueError{Method: "SetInt"})
653657
}
654658
}
655659

@@ -669,7 +673,7 @@ func (v Value) SetUint(x uint64) {
669673
case Uintptr:
670674
*(*uintptr)(v.value) = uintptr(x)
671675
default:
672-
panic(&ValueError{"SetUint"})
676+
panic(&ValueError{Method: "SetUint"})
673677
}
674678
}
675679

@@ -681,7 +685,7 @@ func (v Value) SetFloat(x float64) {
681685
case Float64:
682686
*(*float64)(v.value) = x
683687
default:
684-
panic(&ValueError{"SetFloat"})
688+
panic(&ValueError{Method: "SetFloat"})
685689
}
686690
}
687691

@@ -693,7 +697,7 @@ func (v Value) SetComplex(x complex128) {
693697
case Complex128:
694698
*(*complex128)(v.value) = x
695699
default:
696-
panic(&ValueError{"SetComplex"})
700+
panic(&ValueError{Method: "SetComplex"})
697701
}
698702
}
699703

@@ -703,7 +707,7 @@ func (v Value) SetString(x string) {
703707
case String:
704708
*(*string)(v.value) = x
705709
default:
706-
panic(&ValueError{"SetString"})
710+
panic(&ValueError{Method: "SetString"})
707711
}
708712
}
709713

@@ -788,10 +792,14 @@ type stringHeader struct {
788792

789793
type ValueError struct {
790794
Method string
795+
Kind Kind
791796
}
792797

793798
func (e *ValueError) Error() string {
794-
return "reflect: call of reflect.Value." + e.Method + " on invalid type"
799+
if e.Kind == 0 {
800+
return "reflect: call of " + e.Method + " on zero Value"
801+
}
802+
return "reflect: call of " + e.Method + " on " + e.Kind.String() + " Value"
795803
}
796804

797805
// Calls to this function are converted to LLVM intrinsic calls such as
@@ -865,3 +873,15 @@ func MakeMap(typ Type) Value {
865873
func (v Value) Call(in []Value) []Value {
866874
panic("unimplemented: (reflect.Value).Call()")
867875
}
876+
877+
func (v Value) MethodByName(name string) Value {
878+
panic("unimplemented: (reflect.Value).MethodByName()")
879+
}
880+
881+
func (v Value) Recv() (x Value, ok bool) {
882+
panic("unimplemented: (reflect.Value).Recv()")
883+
}
884+
885+
func NewAt(typ Type, p unsafe.Pointer) Value {
886+
panic("unimplemented: reflect.New()")
887+
}

0 commit comments

Comments
 (0)